%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%巴特沃思数字低通滤波器
%数字滤波器指标
wp=1000*2*pi;ws=2000*2*pi; %滤波器通带和阻带截止频率(Hz)
rp=2;as=35;fs=44100;t=1/fs;
%模拟巴特沃思原型滤波器计算
[N,wc]=buttord(wp,ws,rp,as,'s'); %原型的阶数和边缘频率计算
[ba,aa]=butter(N,wc,'s'); %原型的分子分母系数向量
[bd,ad]=impinvar(ba,aa,fs); %调用脉冲响应不变法函数使成为数字信号
[H,W]=freqz(bd,ad); %计算数字低通滤波器频率响应
figure(10);
plot(W*fs/(2*pi),abs(H));grid on; %画幅频响应图
title('巴特沃思数字低通滤波器幅频响应曲线');ylabel('幅度');xlabel('频率/Hz');%标题设置
axis([0 3000 0 1.2]); %设置坐标范围
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%四种信号
x=0:1/44100:2047/44100; %横坐标取值
sgn=wavrecord(2048,44100,1,'double'); %从声卡取得信号
sgn=sgn'; %信号转置
%sgn=square(1000*2*pi*x);
%sgn=sin(1000*2*pi*x);
hnoise=wgn(1,2048,-10); %高斯白噪声信号,强度为-10dBW
noisesgn=sgn+hnoise; %加有高斯白噪声的信号
lpfnoisesgn=filter(bd,ad,noisesgn); %通过低通滤波器的噪声信号
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%计算信号(sgn)的的统计特性
figure(1);
plot(x,sgn);grid on; %画信号波形
axis([0,0.015,-2,2]);
title('信号波形曲线');
disp('信号均值');
mxsgn=mean(sgn); %信号均值
fprintf('%f\n',mxsgn);
disp('信号方差值');
dsgn=var(sgn); %信号方差值
fprintf('%f\n',dsgn);
disp('信号标准差');
ssgn=std(sgn); %信号标准差
fprintf('%f\n',ssgn);
disp('信号均方值');
ex2sgn=mxsgn^2+dsgn; %信号均方值
fprintf('%f\n',ex2sgn);
rsgn=xcorr(sgn,'unbiased'); %信号的自相关函数
figure(2);
plot(rsgn);grid on; %画自相关函数曲线
title('信号自相关函数曲线');
axis([1000,1500,-1,1]);
fxsgn=fftshift(fft(sgn)); %频谱函数数值
figure(3);
g = floor((-length(fxsgn)/2+0.5):(length(fxsgn)/2-0.5));
d=g*44100/length(fxsgn);
plot(d,abs(fxsgn));
title('信号频谱函数');
axis([0,3000,0,1000]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure(5);
plot(x,hnoise);grid on;
title('高斯白噪声波形曲线');
axis([0,0.015,-2,2]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%加有噪声信号的统计特性
figure(7);
plot(x,noisesgn);grid on; %画加有噪声信号波形
axis([0,0.015,-2,2]);
title('加有噪声信号波形曲线');
disp('加有噪声信号均值');
mxnoisesgn=mean(noisesgn); %加有噪声信号均值
fprintf('%f\n',mxnoisesgn);
disp('加有噪声信号方差值');
dnoisesgn=var(noisesgn); %加有噪声信号方差值
fprintf('%f\n',dnoisesgn);
disp('加有噪声信号标准差');
snoisesgn=std(noisesgn); %加有噪声信号标准差
fprintf('%f\n',snoisesgn);
disp('加有噪声信号均方值');
ex2noisesgn=mxnoisesgn^2+dnoisesgn; %加有噪声信号均方值
fprintf('%f\n',ex2noisesgn);
rnoisesgn=xcorr(noisesgn,'unbiased'); %加有噪声信号的自相关函数
figure(8);
plot(rnoisesgn);grid on; %画自相关函数曲线
title('加有噪声信号自相关函数曲线');
axis([1000,1500,-1,1]);
fxnoisesgn=fftshift(fft(noisesgn)); %频谱函数数值
figure(9);
g = floor((-length(fxnoisesgn)/2+0.5):(length(fxnoisesgn)/2-0.5));
d=g*44100/length(fxnoisesgn);
plot(d,abs(fxnoisesgn));
title('加有噪声信号频谱函数');
axis([0,3000,0,1000]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%通过低通滤波器的加噪声信号
figure(11);
plot(x,lpfnoisesgn);grid on; %画通过低通滤波器的加噪声信号波形
axis([0,0.015,-2,2]);
title('通过低通滤波器的加噪声信号波形曲线');
disp('通过低通滤波器的加噪声信号均值');
mxlpfnoisesgn=mean(lpfnoisesgn); %通过低通滤波器的加噪声信号均值
fprintf('%f\n',mxlpfnoisesgn);
disp('通过低通滤波器的加噪声信号方差值');
dlpfnoisesgn=var(lpfnoisesgn); %通过低通滤波器的加噪声信号方差值
fprintf('%f\n',dlpfnoisesgn);
disp('通过低通滤波器的加噪声信号标准差');
slpfnoisesgn=std(lpfnoisesgn); %通过低通滤波器的加噪声信号标准差
fprintf('%f\n',slpfnoisesgn);
disp('通过低通滤波器的加噪声信号均方值');
ex2lpfnoisesgn=mxlpfnoisesgn^2+dlpfnoisesgn; %通过低通滤波器的加噪声信号均方值
fprintf('%f\n',ex2lpfnoisesgn);
rlpfnoisesgn=xcorr(lpfnoisesgn,'unbiased'); %通过低通滤波器的加噪声信号的自相关函数
figure(12);
plot(rlpfnoisesgn);grid on; %画自相关函数曲线
title('通过低通滤波器的加噪声信号自相关函数曲线');
axis([1000,1500,-1,1]);
fxlpfnoisesgn=fftshift(fft(lpfnoisesgn)); %频谱函数数值
figure(13);
g = floor((-length(fxlpfnoisesgn)/2+0.5):(length(fxlpfnoisesgn)/2-0.5));
d=g*44100/length(fxlpfnoisesgn);
plot(d,abs(fxlpfnoisesgn));
title('通过低通滤波器的加噪声信号频谱函数');
axis([0,3000,0,1000]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%