% 清空数据 clc clear all % 参数设置 f0 = 3500;%信号频率 fs = 8125;%采样频率 T = 1/fs;
t = [0:1/fs:7999/fs]; t1 = [0:2/fs:7999/fs]; t2 = [0:1/(fs*2):(2*7999+1)/(fs*2)]; x = cos(2*pi*f0*t); y = cos(2*pi*f0*t1); x1 = cos(2*pi*f0*t2); % 使用MATLAB自带的播放器进行播放 % sound(x,fs) % sound(y,fs/2) % sound(x1,fs*2)
xpt = zeros(1,16000); M = 1000; for t = 1:1:16000 forj = 0:7999 ifsin(pi*((t-1)/(2*fs)-j*T)/T) == 0 && ((t-1)/(2*fs)-j*T) == 0 xpt(t) = xpt(t) + x(j+1); else xpt(t) = xpt(t) + x(j+1)*(sin(pi*((t-1)/(2*fs)-j*T)/T)/(pi*((t-1)/(2*fs)-j*T)/T)); end end end
%sound(xpt,fs*2)
E = 0; fori = 1:1:15999 E = E + (xpt(i+1)-x1(i))^2; end E = E/16000
t3 = [0:1/fs/24:1]; sig = cos(2*pi*f0*t3);
figure(1) subplot(2,2,1) plot(t3(1:10*24),sig(1:10*24)) title('时域波形') hold on stem(t2(1:20),xpt(1:20)) hold on subplot(2,2,2) plot(t2,xpt-x1) title('时域波形差')
figure(2) subplot(4,2,1) plot(8000*tdraw,20*log10(abs(Xw))) title('原始信号') hold on subplot(4,2,2) plot(8000*t1draw,20*log10(abs(Yw))) title('2倍降采样信号') hold on subplot(4,2,3) plot(8000*t2draw,20*log10(abs(X1w))) title('2倍采样信号') hold on subplot(4,2,4) plot(8000*t2draw,20*log10(abs(Xpw))) title('2倍内插信号') hold on
figure(3) plot(8000*tdraw,20*log10(abs(Xw))) hold on plot(8000*t1draw,20*log10(abs(Yw))) hold on plot(8000*t2draw,20*log10(abs(X1w))) hold on plot(8000*t2draw,20*log10(abs(Xpw))) hold on legend('原始信号','2倍降采样信号','2倍采样信号','2倍内插信号') grid on