MATLAB: How to create low pass filter for audio file

low-pass filter

How do i create a low pass filter for an audio file?
I would like to keep 20 Hz and below Sampling rate 8000 Hz
Thanks

Best Answer

You gave enough information for the Signal Processing Toolbox (with a bit of help from me) to be able to design your filter:
Fs = 8000; % Sampling Frequency (Hz)
Fn = Fs/2; % Nyquist Frequency
Fco = 20; % Passband (Cutoff) Frequency
Fsb = 30; % Stopband Frequency
Rp = 1; % Passband Ripple (dB)
Rs = 10; % Stopband Ripple (dB)
[n,Wn] = buttord(Fco/Fn, Fsb/Fn, Rp, Rs); % Filter Order & Wco
[b,a] = butter(n,Wn); % Lowpass Is Default Design
[sos,g] = tf2sos(b,a); % Second-Order-Section For STability
figure(1)
freqz(sos, 2048, Fs) % Check Filter Performance
Then use the filtfilt function to filter your signal.
I chose a Butterworth design, but if you want a steeper cutoff, consider using a Chebychev design instead. You may want to experiment with the design to get the exact result you want with respect to your signal. See the relevant documentation for details.