FIR filtering using firpm

19 次查看(过去 30 天)
JOHN
JOHN 2013-6-21
评论: RAN 2024-10-10
Hello, i've been asked to make a bandpass FIR filter, in order to filter a wav file. Firstly the wav file had saple rate Fs=20000 and i had to convert it at Fs=8000. I did that using these commands: [zeroA, Fs] = wavread('C:\Users\matlab\sounds\ZA');% to load file ZA.wav Z=zeroA(1:2.5:end);%to convert the sample rate. Now i have to design a band pass FIR filter using the command firpm, in order to eliminate the DC at 60hz and the high frequency noise. the filter features are: stopband: 0=<|F|<=100 Hz, transition zone: 100=<|F|<=200 Hz, passband: 200=<|F|<=4000 Hz. Then i have to filter the wav file whith this FIR filter. I woulb be grateful if you could help me. Thanks in advance.

回答(1 个)

Akshat Dalal
Akshat Dalal 2024-8-22
Hi John,
You can use the 'firpm' function to create a bandpass FIR filter to eliminate DC at 60 Hz and high-frequency noise. The steps for designing the filter and applying it to a WAV file are given below:
  1. Design the Bandpass FIR Filter: Given your specifications, you need to create a filter that stops frequencies below 100 Hz and above 4000 Hz, with a transition band from 100 Hz to 200 Hz on the low end. This can be achieved using the 'firpm' function in MATLAB. Please refer to the following documentation for more information: https://www.mathworks.com/help/signal/ref/firpm.html
  2. Apply the Filter to the WAV File: Once you have designed the filter, you can apply it to your downsampled WAV file using the filter function. Please refer to the following documentation for more information: https://www.mathworks.com/help/matlab/ref/filter.html
I am also attaching a sample code for the above steps:
% Define filter specifications
Fs = 8000; % Sampling frequency
Fstop1 = 100; % First stopband edge
Fpass1 = 200; % First passband edge
Fpass2 = 4000; % Second passband edge
Fstop2 = 4100; % Second stopband edge
Astop1 = 60; % First stopband attenuation (dB)
Apass = 1; % Passband ripple (dB)
Astop2 = 60; % Second stopband attenuation (dB)
% Normalize frequencies to Nyquist frequency (Fs/2)
nyquist = Fs / 2;
frequencies = [0 Fstop1 Fpass1 Fpass2 Fstop2 nyquist] / nyquist;
amplitudes = [0 0 1 1 0 0];
weights = [10^(Astop1/20) 1 10^(Astop2/20)];
% Design the filter using firpm
n = 100; % Filter order (adjust as needed)
b = firpm(n, frequencies, amplitudes, weights);
% Load and downsample the WAV file
[zeroA, originalFs] = audioread('C:\Users\matlab\sounds\ZA.wav');
Z = zeroA(1:2.5:end); % Downsample to 8000 Hz
% Filter the signal
filtered_signal = filter(b, 1, Z);
% Save the filtered signal to a new WAV file
audiowrite('C:\Users\matlab\sounds\ZA_filtered.wav', filtered_signal, Fs);
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Digital Filter Design 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by