基於FIR的音樂加噪降噪及頻譜分析

具體關於FIR濾波的原理相關的內容可以看一下其他都有詳細的介紹,這裏僅給出程序有詳細的備註信息,能夠看懂。

 

 

% 程序名稱:c.m
% 程序功能:利用FLATTOPWIN設計的FIR濾波對語音信號進行濾波去噪
%audioread()
[x,fs]=audioread('Lemon.wav');  % 輸了參數爲文件的全路徑和文件名,輸出的第一個參數是每個樣本的值,fs是生成該波形文件時的採樣率,bits是波形文件每樣本的編碼位數。
sound(x,fs);      % 按指定的採樣率和每樣本編碼位數回放
N=length(x);       % 計算信號X的長度
fn=2200;            % 單頻噪聲頻率
t=0:1/fs:(N-1)/fs;         % 計算時間範圍,樣本數除以採樣頻率
x=x(:,1)';               % 將雙聲道轉爲單聲道
y=x+0.1*sin(fn*2*pi*t);     % 加噪聲
sound(y,fs);         % 應該可以明顯聽出有尖銳的單品嘯叫聲
X=abs(fft(x));  Y=abs(fft(y));    % 對原始信號和加噪聲信號進行fft變換,取幅度值
X=X(1:N/2); Y=Y(1:N/2);       % 截取前半部分
deltaf=fs/N;            % 計算頻譜的譜線間隔
f=0:deltaf:fs/2-deltaf;     % 計算頻譜頻率範圍
figure(1)

subplot(2,2,1);plot(t,x);xlabel('時間t');
ylabel('幅度');title('原始語音信號');
axis([0,4,-1.5,1.5]);

subplot(2,2,2);plot(f,X);xlabel('頻率f');ylabel('幅度譜');
title('原始語音信號幅度譜');
axis([-10,6000,0,700]);
subplot(2,2,3);plot(t,y);xlabel('時間');ylabel('幅度');
title('加干擾後的語音信號');
axis([0,4,-1.5,1.5]);
subplot(2,2,4);plot(f,Y);xlabel('頻率 f');ylabel('幅度譜');
title('加干擾後的語音信號幅度譜');
axis([-10,6000,0,700]);
 
fpd=2100;fsd=2150;fsu=2250;fpu=2300;Rp=1;As=30;      % 阻帶濾波器設計指標
fcd=(fpd+fsd)/2;fcu=(fpu+fsu)/2;df=min((fsd-fpd),(fpu-fsu));  % 計算上下邊帶中心頻率和頻率間隔
wcd=fcd/fs*2*pi;wcu=fcu/fs*2*pi;dw=df/fs*2*pi;    %將Hz爲單位的模擬頻率換算爲rad爲單位的數字頻率 
wsd=fsd/fs*2*pi;wsu=fsu/fs*2*pi;
M=ceil(6.2*pi/dw)+1;      %計算flattopwin窗設計濾波器時需要的階數
n=0:M-1;         % 定義時間範圍
w_fla=(flattopwin(M));    % 產生M階的flattopwin窗
hd_bs=ideal_lp(wcd,M)+ideal_lp(pi,M)-ideal_lp(wcu,M); 
% 調用自編函數計算理想阻帶濾波器的脈衝響應
h_bs=w_fla'.*hd_bs;       % 用窗口法計算實際濾波器脈衝響應
[db,mag,pha,grd,w]=freqz_m(h_bs,1);  % 調用自編函數計算濾波器的頻率特徵
 
figure(2)
subplot(2,2,1);plot(w,db);title('濾波器幅度響應圖');
xlabel('w/pi');ylabel('db');
axis([0,0.8,-60,10]);
line([0,2],[-As,-As],'color','y','linestyle','--','LineWidth',2);
line([0,2],[-Rp,-Rp],'color','g','linestyle','--','LineWidth',2);
line([wsd,wsd],[-30,10],'color','r','linestyle','--','LineWidth',2);
line([wsu,wsu],[-30,10],'color','r','linestyle','--','LineWidth',2);
subplot(2,2,2);plot(w,mag);title('濾波器幅度響應圖');
xlabel('w/pi');ylabel('幅度mag');
axis([0,1,-0.5,1.5]);
subplot(2,2,3);plot(w,pha);title('濾波器幅度響應圖');
xlabel('w/pi');ylabel('相位pha');
axis([0,1,-4,4]);
subplot(2,2,4);stem(n,h_bs);title('濾波器幅度響應圖');
xlabel('w/pi');ylabel('db');
axis([0,1500,0,1]);
 
y_fil=fftfilt(h_bs,y);  %用設計好的濾波器對y進行濾波
Y_fil=fft(y_fil);Y_fil=Y_fil(1:N/2);  %計算頻譜取前一半
figure(3)
subplot(3,2,1);plot(t,x);xlabel('時間t');ylabel('幅度');


title('原始語音信號');
subplot(3,2,2);plot(f,X);xlabel('頻率f');ylabel('幅度譜');
title('原始語音信號幅度譜');
axis([0,4000,0,600]);
subplot(3,2,3);plot(t,y);xlabel('時間t');ylabel('·幅度');
title('加干擾後語音信號');
subplot(3,2,4);plot(f,Y);xlabel('頻率f');ylabel('幅度譜');
title('加干擾後語音信號幅度譜');
axis([0,4000,0,600]);
subplot(3,2,5);plot(t,y_fil);xlabel('時間t');ylabel('幅度');
title('濾波後語音信號');
subplot(3,2,6);plot(f,Y_fil);xlabel('頻率f');ylabel('幅度譜');
title('濾波後語音信號幅度譜');
axis([0,4000,0,600]);
figure(4)
subplot(2,1,1);plot(f,20*log10(Y));grid on; 
subplot(2,1,2);plot(f,20*log10(Y_fil));grid on; 
sound(x,fs);   % 播放原始信號
%audiowrite('Lemon_noise.wav',x,fs);
sound (y_fil,fs);  %播放濾波後的信號
 
[C,B,A]=dir2fs(h_bs);

實驗結果

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章