【源码】基于Hamming窗的FIR滤波器降噪

【源码】基于Hamming窗的FIR滤波器降噪

用FIR滤波器(带通、带阻)降低高斯白噪声

To reduce White Gaussian Noise with FIR Filter (Bandpass, Bandstop)

使用带通和带阻滤波器进行实验的降噪演示。

Demonstration of Noise Reduction using Bandpass and Bandstop Filter.

输入信号首先被带通滤波器过滤,然后再经过带阻滤波器。

The input is being filtered by Bandpass Filter first then Bandstop Filter.

第32到41行可以屏蔽注释,以验证您自己的计算。

Line 32 to 41 can be un-comment to verify with your own calculation.

clc, close all

%% Initialization

Fs = 6000; % Sampling Rate

T = 1/Fs;

S = 999; % No of Samples

n = 0:1:S;

N = length(n);

x = (exp(0.2)cos(2pi1000nT))+(sqrt(3)cos(2pi1700nT));

xn = awgn(x,1,‘measured’); % adding Gaussian Noise

t = n/Fs; % Time series

%Applying FFT on noisy Signal for DFT coefficien

xfft = fft(xn); % DFT coefficient

N1 = length(xn);

k = [0:(N1-1)/2];

f = Fs*k/N1; % formula for frequency bin

y = (2*abs(xfft))/N1; % Amplitude Spectrum k>0

y(1) = y(1)/2; % Amplitude Spectrum k=0

p = (2*abs(xfft)).2/N.2; % Power Spectrum k>0

p(1) = p(1)/2; % Power Spectrum k=0

%% Bandpass Filter : passband 0~999 Hz, 1701 Hz and above

FL = 999/(Fs/2);

FH = 1701/(Fs/2);

H = fir1(100,[FL FH],‘bandpass’); % Bandpass using Hamming window

%% Bandstop Filter : stopband 999-1701 Hz

FL1 = 1001/(Fs/2);

FH1 = 1699/(Fs/2);

H1 = fir1(100,[FL1 FH1],‘stop’); % Bandstop using Hamming window

% %Plot FIR filter Coefficients Bandpass

% figure(2)

% subplot(2,1,1)

% stem(1:length(H),H)

% xlabel(‘n’),ylabel(‘hw(n)’),title(‘FIR Filter Coefficient Bandpass’),grid on;

%

% %Plot FIR filter Coefficients Bandstop

% subplot(2,1,2)

% stem(1:length(H1),H1)

% xlabel(‘n’),ylabel(‘hw(n)’),title(‘FIR Filter Coefficient Bandstop’),grid on;

%% Apply both filter on the Signal

filtered_xn = filter(H,1,xn); % apply bandpass to the noisy signal xn

filtered_xn1 = filter(H1,1,filtered_xn); % apply bandstop to the filtered_xn

y1 = (2*abs(fft(filtered_xn1)))/N; % Amplitude Spectrum k>0

y1(1) = y(1)/2; % Amplitude Spectrum k=0

%% Plot all waveforms

figure(1)

% input signal

subplot(2,2,1);

plot(n,x);

xlabel(‘No of Samples’),ylabel(‘x(n)’),title(‘Input Digital Signal of x(n)’),grid on;

% Single Sided Amplitude Spectrum

subplot(2,2,2);

plot(f,y(1:N/2));

xlabel(‘Frequency(Hz)’),ylabel(‘Amplitude |A(k)|’),title(‘Single Sided Amplitude Spectrum (input)’),grid on;

% Filtered Signal

subplot(2,2,3);

plot(1:N,filtered_xn1);

xlabel(‘No of Samples’),ylabel(‘Sample Value’),title(‘Filtered Signal’),grid on;

% Single Sided Amplitude Spectrum (filtered)

subplot(2,2,4);

plot(f,y1(1:N/2));

xlabel(‘Frequency(Hz)’),ylabel(‘Amplitude |A(k)|’),title(‘Single Sided Amplitude Spectrum (filtered)’),grid on;

% Magnitude & Phase Response of Bandpass and Bandstop

figure(3)

freqz(H)

title(‘Magnitude & Phase Response (Bandpass)’);

figure(31)

subplot (2,1,2)

freqz(H1)

title(‘Magnitude & Phase Response (Bandstop)’);

更多精彩文章请关注公众号:【源码】基于Hamming窗的FIR滤波器降噪