📄 samrate_change.m
字号:
% 采样率转换function:
% output = samrate_change(x,fx,fy,inter,dec)
%
% x--source data
% fx--sample rate of x
% fy--sample rate of output
% inter--interpolate factor
% dec--decimation factor
% gain--内插后有inter大小的幅度丢失,所以该值设置和inter一样
% output--output data with sample rate fy
function [output] = samrate_change(x,fx,fy,inter,dec)
%sample rate
fs = fx;
fs_o = fy;
I = inter;
D = dec;
len = length(x);
% gain--内插后有inter大小的幅度丢失,所以该值设置和inter一样
gain = inter;
%原始输入
t = 0:1/fs:(len-1)/fs;
figure;subplot(2,1,1);plot(t,x);title('source');
y = fft(x);
f =(0:length(y)-1)*fs/length(y);
subplot(2,1,2);plot(f,abs(y));
% subplot(3,1,3);pwelch(x);
%内插
len_i = len*I;
x_i=zeros(len_i,1);
for i = 0:len-1
x_i(I*i+1,1) = x(i+1,1);
end
t_i = 0:1/(I*fs):(len_i-1)/(I*fs);
% figure;subplot(2,1,1);plot(t_i,x_i);title('interplot');
y_i = fft(x_i);
f_i =(0:length(y_i)-1)*fs*I/length(y_i);
% subplot(2,1,2);plot(f_i,abs(y_i));
% subplot(3,1,3);pwelch(x_i);
%低通滤波,包括镜象滤波和抗混淆滤波
Rp = 3;
Rs = 40;
Fs = fs*I;
f = [fs_o/2-100,fs_o/2];
m = [1 0];
dev = [(10^(Rp/20)-1)/(10^(Rp/20)+1) 10^(-Rs/20)];
[n,fo,mo,w] = remezord(f,m,dev,Fs);
mfir = remez(n+1,fo,mo,w);
% figure;freqz(mfir,1,1024,Fs);
x_f = conv(mfir,x_i);
t_f = 0:1/Fs:(length(x_f)-1)/Fs;
% figure;subplot(2,1,1);plot(t_f,x_f);title('filter out');
y_f = fft(x_f);
f_f=(0:length(y_f)-1)*Fs/length(y_f);
% subplot(2,1,2);plot(f_f,abs(y_f));
% subplot(3,1,3);pwelch(x_f);
%抽取
len_d = round(length(x_f)/D);
for i=1:len_d
out(i,1) = x_f(D*(i-1)+1)*gain;
end
t_d = 0:1/fs_o:(length(out)-1)/fs_o;
figure;subplot(2,1,1);plot(t_d,out);title('out');
y_d = fft(out);
f_d=(0:length(y_d)-1)*fs_o/length(y_d);
subplot(2,1,2);plot(f_d,abs(y_d));
% subplot(3,1,3);pwelch(out);
output = out;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -