How can I get the transfer function for the three filters in this code? and how to plot the magnitude spectrum for the filters transfer function?
3 views (last 30 days)
Show older comments
%Part 1 - (a, b, c)
[x, fs] = audioread('audio.wav')
x = x(:, 1);
n = length(x)
t = (0:n-1)/fs
n/fs
%Figure 1 - Time domain representation
figure(1)
plot(t, x)
grid on
xlabel('Time(s)')
ylabel('Amplitude')
title('Time domain ')
%Figure 2 - Spectrogram
figure(2)
spectrogram(x, 1024, 512, 1024, fs, 'yaxis')
title('Spectrogram')
%Figure 3 - Power spectrum density
figure(3)
w = hanning(n, 'periodic');
periodogram(x, w, n, fs, 'power')
title('power spectrum density')
%Play the audio
sound(x,fs)
%Part 2 - d
% define filters
freq = linspace(1000,(fs/2),500);
% 1 - LPF FIR / cutoff frequency 3 KHz
N = 64;
fc_lp = 3000;
B_lp = fir1(N,2*fc_lp/fs);
h=freqz(B_lp,1,freq,fs);
m_lp=20*log10(abs(h));
% 2 - BPF FIR / cutoff frequencies 2 and 5 KHz
N = 64;
fc_low = 2000;
fc_high = 5000;
B_bp = fir1(N,2*[fc_low fc_high]/fs);
h=freqz(B_bp,1,freq,fs);
m_bp=20*log10(abs(h));
% 3 - HPF FIR / cutoff frequency 4 KHz
N = 64;
fc_high = 4000;
B_hp = fir1(N,2*fc_high/fs,'high');
h=freqz(B_hp,1,freq,fs);
m_hp=20*log10(abs(h));
% apply filters on audio file
%LPF
x_lp = filter(B_lp,1,x); % filtered by LPF
figure(4)
plot(t,x_lp)
grid on
xlabel('Time(s)')
ylabel('Amplitude')
title('Time domain for LPF')
figure(5)
spectrogram(x_lp, 1024, 512, 1024, fs, 'yaxis')
title('Spectrogram - LPF filtered signal')
figure(6)
w = hanning(n, 'periodic');
periodogram(x_lp, w, n, fs, 'power')
title('LPF power spectrum density')
%BPF
x_bp = filter(B_bp,1,x); % filtered by BPF
figure(7)
plot(t,x_bp)
grid on
xlabel('Time(s)')
ylabel('Amplitude')
title('Time domain for BPF')
figure(8)
spectrogram(x_bp, 1024, 512, 1024, fs, 'yaxis')
title('Spectrogram - BPF filtered signal')
figure(9)
w = hanning(n, 'periodic');
periodogram(x_bp, w, n, fs, 'power')
title('BPF power spectrum density')
x_hp = filter(B_hp,1,x); % filtered by HPF
figure(10)
plot(t,x_hp)
grid on
xlabel('Time(s)')
ylabel('Amplitude')
title('Time domain for HPF')
figure(11)
spectrogram(x_hp, 1024, 512, 1024, fs, 'yaxis')
title('Spectrogram - HPF filtered signal')
figure(12)
w = hanning(n, 'periodic');
periodogram(x_hp, w, n, fs, 'power')
title('HPF power spectrum density')
0 Comments
Answers (1)
Star Strider
on 12 Dec 2021
The numerator of the transfer functions are the outputs of the fir1 calls (and the denominator is always 1).
For the first filter, this is already done for you —
% 1 - LPF FIR / cutoff frequency 3 KHz
N = 64;
fc_lp = 3000;
B_lp = fir1(N,2*fc_lp/fs);
h=freqz(B_lp,1,freq,fs);
m_lp=20*log10(abs(h));
I didn’t check the code carefully, however it appears the other filters are created and analysed the same way.
.
2 Comments
Star Strider
on 12 Dec 2021
‘so from this B_lp is the tranfer function?’
It’s the numerator of the transfer function.
See the documentation on the fir1 function for details as to what it does, what it returns as outputs, and how to use the information it provides.
‘and how do i get the magnitude spectrum graph from it?’
That’s also given in various locations in the original code, as well as the section I quoted from it.
See the documentation for the freqz function for a full explanation of how it works, what it does, and how to get information from it.
.
See Also
Categories
Find more on Filter Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!