matlab filter design
TRANSCRIPT
The purpose of this project is to design the filter using MATLAB to filter this signal below
The task to complete this project is:
1. Generate the signal above using Matlab.
2. Plot the DFT of this signal.
3. Design a digital Butterworth LPF to remove all frequencies except 1000 Hz.
4. Design a digital Chebyshev HPF filter to maintain only the 5000Hz frequency.
5. Design a BPF of your choice to remove all frequencies except 3000Hz.
From the signal given, the maximum frequency is 5000Hz
In order to take the sample of the signal and to avoid aliasing, the sample frequency selected is
Fs ≥ 2FN = 15000Hz
The sample time will be 1/Fs = 0.0067ms
The sample for 0 ≤ t ≤ 10ms is N = 10ms/Ts = 150 samples
1
1. Generate x(t) in MATLAB
a) MATLAB code
b) Output waveform
2
Compute figure 2
Compute figure 3
Figure 2 : Signal Corrupted with Zero-Mean Random Noise of x(t)
Figure 1 : Single-Sided Amplitude Spectrum of x(t)
Figure 1: signal x(t)
2) Plot the DFT of x(t)
Basically, to compute the DFT of a time signal, x(t), we need to transform the signal into discrete
– time signal , x[n]. After that, the x[n] will be transform into Fourier transform which is X(w) before
transform to DFT of X(k). The X(k) value was compute with the length of N-1 which N is the number of
sample from X(w). For this signal, L = N-1 = 149.
a) MATLAB code
b) Output waveform
3
Show value of X[k] from k=0 to 149
Figure 4 Magnitude of X[k] Figure 5 Phase of X[k]
c) Value of X[k]
k
value
11121314151617181920
3.4894 - 1.1269i 0.3217 - 0.0968i 0.1710 - 0.0469i 0.1175 - 0.0286i 0.0902 - 0.0188i0.0735 - 0.0125i 0.0623 - 0.0079i 0.0542 - 0.0042i 0.0480 - 0.0012i 0.0431 + 0.0015i
4
k value21222324252627282930..
0.0390 + 0.0039i 0.0356 + 0.0062i 0.0326 + 0.0084i 0.0299 + 0.0106i 0.0274 + 0.0129i0.0250 + 0.0152i 0.0227 + 0.0176i 0.0204 + 0.0203i 0.0180 + 0.0232i 0.0154 + 0.0264i
…………..
k value0123456789
10
1.0e+002 -0.0293 -0.0295 + 0.0016i - 0.0301 + 0.0032i -0.0313 + 0.0049i -0.0330 + 0.0067i-0.0353 + 0.0086i -0.0385 + 0.0107i -0.0427 + 0.0132i -0.0485 + 0.0160i -0.0565 + 0.0196i
k value141142143144145146147148149
-0.0565 - 0.0196i -0.0485 - 0.0160i -0.0427 - 0.0132i -0.0385 - 0.0107i -0.0353 - 0.0086i-0.0330 - 0.0067i -0.0313 - 0.0049i -0.0301 - 0.0032i -0.0295 - 0.0016i
FILTER DESIGNING APPROACH
To design the filter, there is several basic information that needs to be focus. The first
step to design a filter is to determine the filter’s type whether it is ‘lowpass’, ‘highpass’ or
‘bandpass’. After that, we need to find the cutoff frequency of the filter by referring to its ideal
design. In this MATLAB, the function that was provided such as buttord(for butterworth) and
cheb1ord(for chebyshev type I) will find the value of order,n and cut off frequency, Wn for the
filter with the given passband frequency, stopband frequency and sample frequency. After the
value of n an Wn has been obtained, the function butter(for butterworth) and cheby1(fo
chebyshev type I) will create the filter equation based on the information found in previous
step. The design may consist of analog domain filter or digital domain filter. To validate that the
filter designed is correct, the original signal, x(t) will be convolved with filter signal, h[n] to
obtain the output signal, y[n]. The magnitude spectrum of the output signal will validate
whether the filter has function as it’s needed or not.
5
3) Design a digital Butterworth LPF to remove all frequencies except 1000 Hz.
To design this filter, we need to determine the cutoff frequency which is 1000Hz
So the pass band will be, Wp = 1000/7500 and the stopband is Ws = 2000/7500
The filter contain less than 3dB ripple in the pass band and 60dB of attenuation in the stopband
From the code generated, the order, n and the new cutoff frequency, Wn is;
n = 10 and Wn = 0.1398
a) MATLAB code
6
close all;clear all;f1=1000;f2=3000;f3=5000;fs=15000;t=0:1/fs:.01;
%original signalx1=5*cos(2*pi*f1*t); x2=2*cos(2*pi*f2*t); x3=.5*cos(2*pi*f3*t); Xw=x1+x2+x3;
%butterworth lowpass FilterWp = 1000/7500; %Passband corner frequencyWs = 2000/7500; %Stopband corner frequency Rp = 3; %Passband ripple, in decibelsRs = 60; %Stopband attenuation, in decibels
%Returns the lowest order n of the butterworth filter,%and the corresponding cutoff frequencies, Wn[n wn]=buttord(Wp,Ws,Rp,Rs)[b a]=butter(n,wn,'low')[z,p,k]= butter(n,wn,'low');figure(1); plot([b a]); title('filter signal');
%Converts a discrete-time zero-pole-gain representation of a given digital%filter to an equivalent second-order section representation.[sos,g]=zp2sos(z,p,k);
%Discrete-time, second-order section, direct-form II filterHd = dfilt.df2sos(sos,g); y=filter(Hd,Xw); %Passing signal through filter
%plotting%magnitude and phase responsefvtool(Hd);title('Butterworth lowpass Filter')fvtool(y);title('Frequency domain Filtered Y');
%Time Domainsubplot(511);plot(t,x1,'r');hold on;stem(t,x1);title('Original X1=5cos(2*pi*1000*t)');subplot(512);plot(t,x2,'r');hold on;stem(t,x2);title('Original X2=2cos(2*pi*3000*t)');subplot(513);plot(t,x3,'r');hold on;stem(t,x3);title('Original X3=0.5cos(2*pi*5000*t)');subplot(514);plot(t,Xw,'r');hold on;stem(t,Xw);title('X1+X2+X3=5cos(2*pi*1000*t)+2cos(2*pi*3000*t)+0.5cos(2*pi*5000*t)');subplot(515);plot(t,y,'r');hold on;stem(t,y);title('Filtered Y signal');
Determine order, n and the new cutoff frequency Wn
Show filter window tool
b) Output waveform
7
Figure 6 : magnitude response of filter
8
Figure 7 : phase response of filter
Figure 8 : signal of the filter
c) Filter validation
Figure 9: Shows that the filtered signal is almost the same with X1 signal with 1000Hz and magnitude of 5 which means that X2 and X3 is being filtered.
Alternative method:
To validate the filter that have been designed, the input signal, x(t) was convolved with the filter, h[n] using the MATLAB code as below;
9
ts=0:1:100;x=5*cos(2*pi*f1*ts)+2*cos(2*pi*f2*ts)+0.5*cos(2*pi*f3*ts);res=2*pi/360;w=0:res:2*pi-res;lpf=[b a]
out=conv(x,lpf);figure(1);plot(out)
out_mag=abs(out);figure(2);plot(w(1:75).*15000/(2*pi),out_mag(1:75))
[b a] Is the filter equation
The output waveform after the signal has been filtered out is;
From figure 11, it shows that the frequency at 1000Hz is remain and proved that this filter is valid.
4) Design a digital Chebyshev HPF filter to maintain only the 5000Hz frequency.
In order to design the HPF, the bandpass and stoppass band was first determined, to get the frequency
at 5000Hz only
The band pass frequency, Wp = 5000/7500
Stop band frequency, Ws = 4000/7500
The Rp, ripple at pass band is 3dB and Rs, ripple at stop band is 40dB
The order, n and the new cutoff frequency is:
n = 6 and Wn = 0.6667
10
Figure 10: output signal after filtered Figure 11: magnitude spectrum of the filtered signal
Cheby1 indicate that 1st order chebyshev is used
a) MATLAB code
b) Output waveform
11
close all;clear all;f1=1000;f2=3000;f3=5000;fs=15000;t=0:1/fs:.01;
%original signalx1=5*cos(2*pi*f1*t); x2=2*cos(2*pi*f2*t); x3=.5*cos(2*pi*f3*t); Xw=x1+x2+x3;
%Chebyshev Type 1 highpass FilterWp = 5000/7500; %Passband corner frequencyWs = 4500/7500; %Stopband corner frequency Rp = 3; %Passband ripple, in decibelsRs = 40; %Stopband attenuation, in decibels
%Returns the lowest order n of the Chebyshev type I filter,%and the corresponding cutoff frequencies, Wn[n,Wn] = cheb1ord(Wp,Ws,Rp,Rs); [b,a] = cheby1(n,Rp,Wn,'high');[z,p,k]= cheby1(n,Rp,Wn,'high');
%Converts a discrete-time zero-pole-gain representation of a given digital%filter to an equivalent second-order section representation.[sos,g]=zp2sos(z,p,k);
%Discrete-time, second-order section, direct-form II filterHd = dfilt.df2sos(sos,g); y=filter(Hd,Xw); %Passing signal through filter
%Plotting%Frequency Domainfvtool(Hd);title('Chebyshev Type I Highpass Filter')fvtool(x1);title('Frequency domain X1');fvtool(x2);title('Frequency domain X2');fvtool(x3);title('Frequency domain X3');fvtool(Xw);title('Frequency domain X1+X2+X3');fvtool(y);title('Frequency domain Filtered Y');
%Time Domainsubplot(511);plot(t,x1,'r');hold on;stem(t,x1);title('Original X1=5*cos(2*pi*1000*t)');subplot(512);plot(t,x2,'r');hold on;stem(t,x2);title('Original X2=2*cos(2*pi*3000*t)');subplot(513);plot(t,x3,'r');hold on;stem(t,x3);title('Original X3=0.5*cos(2*pi*5000*t)');subplot(514);plot(t,Xw,'r');hold on;stem(t,Xw);title('X1+X2+X3=5cos(2*pi*1000*t)+2cos(2*pi*3000*t)+0.5cos(2*pi*5000*t)');subplot(515);plot(t,y,'r');hold on;stem(t,y);title('Filtered Y signal');
Figure 12: magnitude response
c) Filter validation:
12
Figure 13: phase response
Figure 14: the magnitude and phase response of the filtered signal.
Figure 15: the filtered signal is almost the same with X3 signal with 5000Hz and magnitude of 0.5 which means that X1 and X2 is being filtered.
5) Design a BPF of butterworth filter to remove all frequencies except 3000Hz
13
To design this filter so that only 3000Hz frequency remains, the passband needed is from 3000Hz to
3500Hz and the stop band is 500Hz different before and after the pass band. From the MATLAB code
generates, the order, n and new cutoff frequency, Wn is;
n = 9 and Wn = [0.3975 0.4693]
The Rp, ripple at the pass band is 3dB and the ripple at stop band is 80dB
a) MATLAB code
14
close all;clear all;f1=1000;f2=3000;f3=5000;fs=15000;t=0:1/fs:.01; %original signalx1=5*cos(2*pi*f1*t); x2=2*cos(2*pi*f2*t); x3=.5*cos(2*pi*f3*t); Xw=x1+x2+x3; %butterworth Bandpass FilterWp = [3000 3500]/7500; %Passband corner frequencyWs = [2500 4000]/7500; %Stopband corner frequency Rp = 3; %Passband ripple, in decibelsRs = 80; %Stopband attenuation, in decibels %Returns the lowest order n of the butterworth filter,%and the corresponding cutoff frequencies, Wn[n wn]=buttord(Wp,Ws,Rp,Rs)[b a]=butter(n,wn)[z,p,k]= butter(n,wn);figure(1); plot([b a]); title('filter signal'); %Converts a discrete-time zero-pole-gain representation of a given digital%filter to an equivalent second-order section representation.[sos,g]=zp2sos(z,p,k); %Discrete-time, second-order section, direct-form II filterHd = dfilt.df2sos(sos,g); y=filter(Hd,Xw); %Passing signal through filter %plotting%magnitude and phase responsefvtool(Hd);title('Butterworth bandpass Filter')fvtool(y);title('Frequency domain Filtered Y'); %Time Domainsubplot(511);plot(t,x1,'r');hold on;stem(t,x1);title('Original X1=5cos(2*pi*1000*t)');subplot(512);plot(t,x2,'r');hold on;stem(t,x2);title('Original X2=2cos(2*pi*3000*t)');subplot(513);plot(t,x3,'r');hold on;stem(t,x3);title('Original X3=0.5cos(2*pi*5000*t)');subplot(514);plot(t,Xw,'r');hold on;stem(t,Xw);title('X1+X2+X3=5cos(2*pi*1000*t)+2cos(2*pi*3000*t)+0.5cos(2*pi*5000*t)');subplot(515);plot(t,y,'r');hold on;stem(t,y);title('Filtered Y signal');
b) Output waveform
15
Figure 16: magnitude response
Figure 17: phase response
c) Filter validation
Figure 19: the filtered signal is almost the same with X2 signal with 3000Hz and magnitude of 2 which means that X1 and X3 is being filtered.
16
Figure 18: filter signal
Alternative method:
To validate the filter designed, the same approach is use by using the same MATLAB code as below;
The output waveform for the signal that has been filtered out is;
From figure 21, it shows that the frequency at 3000Hz remains and other frequency were eliminated
17
Figure 20: output signal after filtered Figure 21: magnitude spectrum of the signal
ts=0:1:100;x=5*cos(2*pi*1000/15000*ts)+2*cos(2*pi*3000/15000*ts)+0.5*cos(2*pi*5000/15000*ts);res=2*pi/120;w=0:res:2*pi-res;
bpf=[b a]
out=conv(x,bpf);figure(1);plot(out)
out_mag=abs(out);figure(2);plot(w(1:75).*15000/(2*pi),out_mag(1:75))