Algorithm:: All "Enter The Input Sequence: " "Enter The Starting Point of The Sequence:"
Algorithm:: All "Enter The Input Sequence: " "Enter The Starting Point of The Sequence:"
subplot(3,2,4);
• Start the MATLAB software stem(x_axis,y_axis); PROGRAM:
title('Discrete Time Exponential Signal'); clc;
• Assign the time and amplitude values xlabel('n'); close all;
• Use the necessary commands to generate the signal ylabel('Amplitude');
range=input("Unit Impulse Signal\n Enter the range of X-axis: ");
• Plot the graph and give the x label and y label and title it N=input("Discrete Time Sinusoidal Signal\n Enter the Time period N: "); x_axis=(-range:0.001:range);
A=input(" Enter the Amplitude: ");
• Save and run the program y_axis=[zeros(1,1000*range),ones(1,1),zeros(1,1000*range)];
f=input(" Enter the frequency: "); subplot(3,2,1);
x_axis=(0:0.1:N); plot(x_axis,y_axis);
y_axis= A*sin(2*pi*f*x_axis); title('Unit Impulse Signal');
PROGRAM: GENERATION OF SEQUENCE (OR) SIGNAL subplot(3,2,5); xlabel('t');
i) Generate all the signals in discrete - time domain. stem(x_axis,y_axis); ylabel('Amplitude');
title('Discrete Time Sinusoidal Signal');
PROGRAM: xlabel('N'); range=input("\nUnit Step Signal\n Enter the range of X-axis: ");
ylabel('Amplitude'); x_axis=(-range:0.0001:range);
clc;
close all; y_axis=[zeros(1,10000*range),ones(1,10000*range+1)];
N=input("Discrete Time Cosine Signal\n Enter the Time period N: "); subplot(3,2,2);
A=input(" Enter the Amplitude: "); plot(x_axis,y_axis);
range=input("Unit Impulse Signal\n Enter the range of X-axis: "); f=input(" Enter the frequency: ");
x_axis=(-range:1:range); title('Unit Step Signal');
x_axis=(0:0.1:N); xlabel('t');
y_axis=[zeros(1,range),ones(1,1),zeros(1,range)]; y_axis= A*cos(2*pi*f*x_axis);
subplot(3,2,1); ylabel('Amplitude');
subplot(3,2,6);
stem(x_axis,y_axis); stem(x_axis,y_axis);
title('Unit Impulse Signal'); range=input("\nUnit Ramp Signal\n Enter the range of X-axis: ");
title('Discrete Time Cosine Signal'); x_axis=(-range:1:range);
xlabel('n'); xlabel('N');
ylabel('Amplitude'); y_axis=[zeros(1,range),x_axis(range+1:2*range+1)];
ylabel('Amplitude'); subplot(3,2,3);
range=input("Unit Step Signal\n Enter the range of X-axis: "); plot(x_axis,y_axis);
x_axis=(-range:1:range); OUTPUT: title('Unit Ramp Signal');
y_axis=[zeros(1,range),ones(1,range+1)]; xlabel('t');
subplot(3,2,2); ylabel('Amplitude');
stem(x_axis,y_axis);
title('Unit Step Signal'); range=input("\nContinuous Time Exponential Signal\n Enter the Time period t: ");
xlabel('n'); A=input(" Enter the pre-exponential factor: ");
ylabel('Amplitude'); x_axis=(-range/2:0.1:range);
y_axis= A*exp(x_axis);
range=input("Unit Ramp Signal\n Enter the range of X-axis: "); subplot(3,2,4);
x_axis=(-range:1:range); plot(x_axis,y_axis);
y_axis=[zeros(1,range),x_axis(range+1:2*range+1)]; title('Continuous Time Exponential Signal');
subplot(3,2,3); xlabel('t');
stem(x_axis,y_axis); ylabel('Amplitude');
title('Unit Ramp Signal');
xlabel('n'); N=input("\nContinuous Time Sinusoidal Signal\n Enter the Time period T: ");
ylabel('Amplitude'); A=input(" Enter the Amplitude: ");
f=input(" Enter the frequency: ");
range=input("Discrete Time Exponential Signal\n Enter the Time period n: "); x_axis=(0:0.001:N);
A=input(" Enter the pre-exponential factor: "); y_axis= A*sin(2*pi*f*x_axis);
x_axis=(-range/2:0.1:range); subplot(3,2,5);
plot(x_axis,y_axis);
f2=input(" Enter the frequency of Second signal: "); y4= y1+y2; I) COMPUTING AUTO CORRELATION OF THE GIVEN INPUT SEQUENCE:
f3=input(" Enter the frequency of Third signal: "); subplot(4,2,6);
PROGRAM
fs=input(" Enter the Sampling frequency: "); plot(n,y4);
title("Signal-1 + Signal-2 "); clc;
n=(0:N-1); xlabel("n"); close all;
y1=sin(2*pi*(f1/fs)*n); ylabel("Amplitude");
y2=sin(2*pi*(f2/fs)*n); X=input("Enter the input sequence: ");
spx=input("Enter the starting point of the
y3=sin(2*pi*(f3/fs)*n); y5=y1.*y3;
sequence:");
subplot(4,2,7);
subplot(4,2,1); plot(n,y5); len=length(X);
plot(n,y1); title("Signal-1 x Signal-3"); subplot(2,1,1);
title("Signal-1"); xlabel("n"); stem(spx:spx+len-1,X,'fill','r');
xlabel("n"); ylabel("Amplitude"); title("Sequence");
ylabel("Amplitude"); xlabel("n");
ylabel("X[n]");
subplot(4,2,3); [autocorr,lags]=xcorr(X);
plot(n,y2); OUTPUT: lags
title("Signal-2"); autocorr
xlabel("n"); subplot(2,1,2);
stem(lags,autocorr,'fill','b');
ylabel("Amplitude");
title("Autocorrelation of the sequence");
xlabel("n");
subplot(4,2,5); ylabel("Y[n]");
plot(n,y3);
title("Signal-3");
xlabel("n");
ylabel("Amplitude");
nd=(20:N-1+20);
y1d=sin(2*pi*(f1/fs)*nd);
subplot(4,2,2);
plot(nd,y1d);
title("Signal-1 Delayed by 20 samples");
xlabel("n");
ylabel("Amplitude");
na=(-10:N-1-10);
y2a=sin(2*pi*(f2/fs)*na);
subplot(4,2,4);
plot(na,y2a);
title("Signal-2 Advanced by 10 samples");
xlabel("n");
ylabel("Amplitude");
II) FIND AUTOCORRELATION OF A SINUSOIDAL SIGNAL: ALGORITHM – CROSS CORRELATION: III) CROSS-CORRELATION OF TWO DIFFERENT SINUSOIDAL SIGNALS:
OUTPUT:
ly=lx+lh-1;
X= [x, zeros(1,(ly-lx))];
H = [h, zeros(1,(ly-lh))];
y= zeros(1, lx+lh-1);
• Finally plot the convolution sequence and the input sequence in a single window • Perform circular convolution for both the sequences
using subplot function. • Plot the sequence
• Stop the program OUTPUT:
• Display the output sequence
PROGRAM: • Stop
clc;
close all;
PROGRAM:
x=input("ENTER THE INPUT SEQUENCE 1=");
spx=input("ENTER THE STARTING POINT OF SEQUENCE 1="); clc;
lx=length(x); close all;
subplot(1,3,1);
stem(spx:spx+lx-1,x,'fill','b'); x=input("ENTER THE INPUT SEQUENCE 1=");
xlabel('sample'); spx=input("ENTER THE STARTING POINT OF SEQUENCE 1=");
ylabel('Amplitude'); lx=length(x);
title('INPUT SEQUENCE 1'); subplot(1,3,1);
stem(spx:spx+lx-1,x,'fill','r');
h=input("ENTER THE INPUT SEQUENCE 2="); xlabel('sample');
sph=input("ENTER THE STARTING POINT OF SEQUENCE 2="); ylabel('Amplitude');
lh=length(h); title('INPUT SEQUENCE 1');
subplot(1,3,2);
stem(sph:sph+lh-1,h,'fill', 'r'); h=input("ENTER THE INPUT SEQUENCE 2=");
xlabel('sample'); sph=input("ENTER THE STARTING POINT OF SEQUENCE 2=");
ylabel('Amplitude'); lh=length(h);
title('INPUT SEQUENCE 2'); subplot(1,3,2);
stem(sph:sph+lh-1,h,'fill', 'g');
y=cconv(x,h); xlabel('sample');
ly=length(y); ylabel('Amplitude');
subplot(1,3,3); title('INPUT SEQUENCE 2');
stem(spx+sph:spx+sph+ly-1,y,'fill','g');
xlabel('sample'); ly=lx+lh-1;
ylabel('Amplitude'); X= [x, zeros(1,(ly-lx))];
title('CIRCULAR CONVOLUTION'); H = [h, zeros(1,(ly-lh))];
OUTPUT:
Circular convolution using FFT ALGORITHM: stem(t/fns,signal_nquist,'b');
hold on;
ALGORITHM: Input the desired frequency fd (for which sampling theorem is to be verified). plot(t/fns,signal_nquist,'r','LineWidth',0.75);
title("Cosine signal - Nquist Sampled");
• Start the program Generate an analog signal x(t) of frequency fd for comparison.
xlabel("n");
• Enter the x(n) sequence. Generate over sampled, Nyquist & under sampled discrete time signals. ylabel("Amplitude");
• Enter the h(n) sequence. Plot the waveforms and hence prove sampling theorem. %Plot the oversampled signal
subplot(2,2,4);
• Perform circular convolution using FFT function. fos=4*f;
• Plot the values. PROGRAM: %Sampling rate much higher than the Nquist rate
t=(-3*fos:0.1:3*fos);
• Stop the program clc; signal_oversampled=2*cos(2*pi*(f/fos)*t);
close all; stem(t/fos,signal_oversampled,'b');
hold on;
fprintf("\t\t\t\t\t\t\t\t\t\t\t\t"+"Verification of
PROGRAM: plot(t/fos,signal_oversampled,'r','LineWidth',0.75);
Sampling Theorem\n"); title("Cosine signal - Oversampled ");
clc; %Get the input frequency xlabel("n");
close all; f=input("Enter the frequency of the cosine signal: "); ylabel("Amplitude");
x=input("ENTER THE INPUT SEQUENCE 1="); %Plot the real signal fprintf("\nUndersampling => Sampling frequency =
spx=input("ENTER THE STARTING POINT OF SEQUENCE 1="); subplot(2,2,1); "+f/1.5+"\n");
lx=length(x); t=(-3:0.01:3); fprintf("Nquist sampling => Sampling frequency =
subplot(1,3,1); signal=2*cos(2*pi*f*t); "+2*f+"\n");
stem(spx:spx+lx-1,x,'fill','r'); plot(t,signal); fprintf("Oversampling => Sampling frequency =
xlabel('sample'); title("Cosine signal"); "+4*f+"\n");
ylabel('Amplitude'); xlabel("Time(t)");
title('INPUT SEQUENCE 1'); ylabel("Amplitude");
OUTPUT:
h=input("ENTER THE INPUT SEQUENCE 2="); %Plot the undersampled signal
sph=input("ENTER THE STARTING POINT OF SEQUENCE 2="); subplot(2,2,2);
lh=length(h); fus=f/1.5;
subplot(1,3,2); %Sampling rate = f (Less than Nquist rata
stem(sph:sph+lh-1,h,'fill', 'g'); t=(-3*fus:0.1:3*fus);
xlabel('sample'); signal_undersampled=2*cos(2*pi*(f/fus)*t);
ylabel('Amplitude'); stem(t/fus,signal_undersampled,'b');
title('INPUT SEQUENCE 2'); hold on;
plot(t/fus,signal_undersampled,'r','LineWidth',0.75);
ly=lx+lh-1; title("Cosine signal - Undersampled");
X= [x, zeros(1,(ly-lx))]; xlabel("n");
H = [h, zeros(1,(ly-lh))]; ylabel("Amplitude");
circfft=ifft(fft(X).*fft(H));
subplot(1,3,3); %Plot the Nyquist sampled signal
stem(spx+sph:spx+sph+ly-1, circfft, 'fill','b'); subplot(2,2,3);
xlabel('sample'); fns=2*f;
ylabel('Amplitude'); %Sampling rate equal to Nquist rate
title('CIRCULAR CONVOLUTION USING FFT'); t=(-3*fns:0.1:3*fns);
signal_nquist=2*cos(2*pi*(f/fns)*t);
OUTPUT:
LIBRARY FUNCTIONS:
%Plot the oversampled signal
SAMPLING A SINUSOIDAL SIGNAL: subplot(2,2,4); spectrum(x,N) – Magnitude of the power spectra of the signal w.r.t the sampling
PROGRAM: fos=4*f;
frequency
%Sampling rate much higher than the Nquist rate
clc; t=(-3*fos:0.1:3*fos); specplot(y,fs) – To plot the spectrum of the signal
close all; signal_oversampled=sin(2*pi*(f/fos)*t);
stem(t/fos,signal_oversampled,'b'); fft(signal) – Provides the spectral analysis by FFT
fprintf("\t\t\t\t\t\t\t\t\t\t\t\t"+"Verification of hold on;
Sampling Theorem\n"); plot(t/fos,signal_oversampled,'r','LineWidth',0.75);
%Get the input frequency title("Sine signal - Oversampled "); QUESTION:
f=input("Enter the frequency of the sine signal: "); xlabel("n");
ylabel("Amplitude");
%Plot the real signal Generate a signal of 512 samples which consists of three co-sinusoids at frequencies
subplot(2,2,1); fprintf("\nUndersampling => Sampling frequency = f1=1000Hz, f2=1500Hz and f3=2000Hz. The sampling frequency of the signal is 10KHz.
t=(-3:0.01:3); "+f/1.5+"\n"); Observe the spectrum using the command “spectrum”. Also sample the signal exactly
signal=sin(2*pi*f*t); fprintf("Nquist sampling => Sampling frequency = equal to the Nyquist rate, greater than Nyquist rate and less than Nyquist rate.
plot(t,signal); "+2*f+"\n");
title("Sine signal"); fprintf("Oversampling => Sampling frequency =
xlabel("Time(t)"); "+4*f+"\n"); PROGRAM:
ylabel("Amplitude");
clc;
%Plot the undersampled signal OUTPUT: close all;
subplot(2,2,2);
fus=f/1.5; fprintf("\t\t\t\t\t\t\t\t\t\t\t\t"+"Sampling and Effect of
%Sampling rate = f (Less than Nquist rata Aliasing\n");
t=(-3*fus:0.1:3*fus); %Get the input frequency
signal_undersampled=sin(2*pi*(f/fus)*t); f1=input("Enter the frequency of the Cosine signal -1: ");
stem(t/fus,signal_undersampled,'b'); f2=input("Enter the frequency of the Cosine signal -2: ");
hold on; f3=input("Enter the frequency of the Cosine signal -3: ");
plot(t/fus,signal_undersampled,'r','LineWidth',0.75); fs=input("Enter the sampling frequency: ");
title("Sine signal - Undersampled"); N=input("Enter the number of samples: ");
xlabel("n");
ylabel("Amplitude"); %Plot the real signal fs=10kHz
subplot(2,2,1);
%Plot the Nyquist sampled signal n=(0:N-1);
subplot(2,2,3); signal=cos(2*pi*(f1/fs)*n)+cos(2*pi*(f2/fs)*n)+cos(2*pi*(f3/f
fns=2*f; s)*n);
%Sampling rate equal to Nquist rate [y,f]=pspectrum(signal,fs);
t=(-3*fns:0.1:3*fns); plot(f,y);
signal_nquist=sin(2*pi*(f/fns)*t); title("Power Density - Fs=10kHz");
stem(t/fns,signal_nquist,'b'); xlabel("Frequency(Hz)");
hold on; ylabel("Power");
plot(t/fns,signal_nquist,'r','LineWidth',0.75);
title("Sine signal - Nquist Sampled"); %Plot the Nyquist sampled signal
xlabel("n"); subplot(2,2,3);
ylabel("Amplitude"); fs= f3;
n=(0:N-1);
%Butterworth IIR LPF Filter an=angle(h); 2. To design an IIR Butterworth and Chebyshev HPF for the following specification:
[N,Wn]=buttord(omgp,omgs,Ap,As,'s'); %Compute Cutoff
frequency and Order subplot(2,2,2); %Plot the response graphs 0.6.1
disp("Butterworth IIR LPF Filter"); plot(ph/pi,20*log10(m));
fprintf("Order, N = ");disp(N); title('Chebyshev IIR LPF Filter');
fprintf("Cutoff Frequency, Wn = ");disp(Wn); xlabel('Normalised frequency'); transformation.
ylabel('Gain in dB');
%Design a analog Butterworth LPF of Order N and Cutoff subplot(2,2,4);
frequency Wn plot(ph/pi,an); PROGRAM:
[num,den]=butter(N,Wn,'high','s'); title('Chebyshev IIR LPF Filter');
[B,A]=bilinear(num,den,(1/Ts)); xlabel('Normalised frequency'); %IIR- BUTTERWORTH AND CHEBYSHEV HIGHPASS FILTERS USING
%Covert analog filter into digital using Bilinear ylabel('Angle in Radians'); IMPULSE INVARIANT TRANSFORMATION
Transformation close all;
W=0:0.01:pi; clc;
[h,ph]=freqz(B,A,W,'whole'); %Obtain the frequency OUTPUT:
response fprintf("\t\t\t\t\t\t\t\t<strong>IIR- BUTTERWORTH AND
m=abs(h); CHEBYSHEV HIGHPASS FILTERS USING IMPULSE INVARIANT
an=angle(h); TRANSFORMATION </strong>\n");
Ap=input('Enter pass band ripple in dB,Ap : ');
subplot(2,2,1); %Plot the response graphs As=input('Enter stop band attenuation in dB,As : ');
plot(ph/pi,20*log10(m)); Wp=input('Enter pass band edge frequency in rad, Wp : ');
title('Butterworth IIR LPF Filter'); Ws=input('Enter stop band edge frequency in rad, Ws : ');
xlabel('Normalised frequency'); Ts=input('Enter the sampling time period in s,Ts : ');
ylabel('Gain in dB');
subplot(2,2,3); omgp=(Wp/Ts); %Compute pre-warped analog frequency
plot(ph/pi,an); omgs=(Ws/Ts);
title('Butterworth IIR LPF Filter');
xlabel('Normalised frequency'); %Butterworth IIR HPF Filter
ylabel('Angle in Radians'); [N,Wn]=buttord(omgp,omgs,Ap,As,'s'); %Compute Cutoff
frequency and Order
%Chebyshev IIR LPF Filter disp("Butterworth IIR HPF Filter");
[N,Wn]=cheb1ord(omgp,omgs,Ap,As,'s'); %Compute Cutoff fprintf("Order, N = ");disp(N);
frequency and Order fprintf("Cutoff Frequency, Wn = ");disp(Wn);
disp("CHEBYSHEV IIR LPF Filter");
fprintf("Order, N = ");disp(N); %Design a analog Butterworth HPF of Order N and Cutoff
fprintf("Cutoff Frequency, Wn = ");disp(Wn); frequency Wn
[num,den]=butter(N,Wn,'high','s');
%Design a analog Chebyshev LPF of Order N and Cutoff [B,A]=impinvar(num,den,(1/Ts));
frequency Wn %Covert analog filter into digital using Impulse
[num,den]=cheby1(N,Ap,Wn,'high','s'); Invariant Transformation
%Covert analog filter into digital using Bilinear W=0:0.01:pi;
Transformation [h,ph]=freqz(B,A,W,'whole'); %Obtain the frequency
[B,A]=bilinear(num,den,(1/Ts)); response
W=0:0.01:pi; m=abs(h);
[h,ph]=freqz(B,A,W,'whole'); %Obtain the frequency an=angle(h);
response
m=abs(h); subplot(2,2,1); %Plot the response graphs
plot(ph/pi,20*log(m)); QUESTION: %Covert analog filter into digital using Bilinear
title('Butterworth IIR HPF Filter'); Transformation
xlabel('Normalised frequency'); 1. To design a digital Butterworth IIR bandpass filter using bilinear transformation for [B,A]=bilinear(num,den,Fs);
ylabel('Gain in dB'); W=0:0.01:pi;
the given specification:
subplot(2,2,3); [h,ph]=freqz(B,A,W,'whole'); %Obtain the frequency response
plot(ph/pi,an); Lower stop band edge frequency = 0.1 rad, Lower pass band edge frequency = 0.4 rad m=abs(h);
title('Butterworth IIR HPF Filter'); an=angle(h);
xlabel('Normalised frequency'); Upper stop band edge frequency = 0.9 rad, Upper pass band edge frequency = 0.6 rad
ylabel('Angle in Radians'); Ap = 3 dB and As = 18 dB with Fs = 500Hz. subplot(2,1,1); %Plot the response graphs
plot(ph/pi,20*log(m));
%Chebyshev IIR HPF Filter title('Butterworth IIR BPF Filter - Magnitude Response');
[N,Wn]=cheb1ord(omgp,omgs,Ap,As,'s'); %Compute Cutoff xlabel('Normalised frequency');
PROGRAM: ylabel('Gain in dB');
frequency and Order
disp("CHEBYSHEV IIR HPF Filter"); %IIR- BUTTERWORTH BANDPASS FILTER USING BILINEAR subplot(2,1,2);
fprintf("Order, N = ");disp(N); TRANSFORMATION plot(ph/pi,an);
fprintf("Cutoff Frequency, Wn = ");disp(Wn); close all; title('Butterworth IIR BPF Filter - Phase Response');
clc; xlabel('Normalised frequency');
%Design a analog Chebyshev HPF of Order N and Cutoff ylabel('Angle in Radians');
frequency Wn fprintf("\t\t\t\t\t\t\t\t<strong>IIR- BUTTERWORTH BANDPASS
[num,den]=cheby1(N,Ap,Wn,'high','s'); FILTER USING BILINEAR TRANSFORMATION </strong>\n");
%Covert analog filter into digital using Impulse Ap=input('Enter pass band ripple in dB,Ap : '); %Get the
input OUTPUT:
Invariant Transformation
[B,A]=impinvar(num,den,(1/Ts)); As=input('Enter stop band attenuation in dB,As : ');
W=0:0.01:pi; Wpl=input('Enter lower passband edge frequency in rad : ');
[h,ph]=freqz(B,A,W,'whole'); %Obtain the frequency Wpu=input('Enter upper passband edge frequency in rad : ');
response Wsl=input('Enter lower stopband edge frequency in rad : ');
m=abs(h); Wsu=input('Enter upper stopband edge frequency in rad : ');
an=angle(h); Fs=input('Enter the sampling frequency in Hz,Fs : ');
subplot(2,2,2); %Plot the response graphs Wp=[Wpl Wpu]; %Vector of passband edge frequencies
plot(ph/pi,20*log(m)); Ws=[Wsl Wsu]; %Vector of stopband edge frequencies
title('Chebyshev IIR HPF Filter');
xlabel('Normalised frequency'); omgp=(2*Fs)*tan(Wp/2); %Compute pre-warped analog frequency
ylabel('Gain in dB'); omgs=(2*Fs)*tan(Ws/2);
subplot(2,2,4);
plot(ph/pi,an); %Butterworth IIR BPF Filter
title('Chebyshev IIR HPF Filter'); [N,Wn]=buttord(omgp,omgs,Ap,As,'s'); %Compute Cutoff edge
xlabel('Normalised frequency'); frequencies and Order
ylabel('Angle in Radians'); fprintf("\n");
disp("Butterworth IIR BPF Filter");
OUTPUT: fprintf("Order, N = ");disp(N);
fprintf("Cutoff edge frequencies, Wn = ");disp(Wn);
2. To design a digital Chebyshev IIR bandstop filter using impulse invariant W=0:0.001:pi; APPENDIX:
[h,ph]=freqz(B,A,W,'whole'); %Obtain the frequency response
transformation for the given specification: m=abs(h);
1. BUTTERWORTH BPF USING IMPULSE INVARIANT TRANFORMATION
Lower stop band edge frequency = 0.4 rad, Lower pass band edge frequency = 0.1 rad an=angle(h);
METHOD
Upper stop band edge frequency = 0.6 rad, Upper pass band edge frequency = 0.9 rad subplot(2,1,1); %Plot the response graphs
plot(ph/pi,20*log(m));
Ap = 3 dB and A s = 18 dB with Fs = 500Hz. title('Chebyshev IIR BSF Filter - Magnitude Response');
PROGRAM:
xlabel('Normalised frequency');
ylabel('Gain in dB'); %IIR- BUTTERWORTH BANDPASS FILTER USING IMPULSE INVARIANT
PROGRAM: subplot(2,1,2); TRANSFORMATION
plot(ph/pi,an); close all;
%IIR- CHEBYSHEV BANDSTOP FILTER USING IMPULSE INVARIANT clc;
TRANSFORMATION title('Chebyshev IIR BSF Filter - Phase Response');
close all; xlabel('Normalised frequency');
ylabel('Angle in Radians'); fprintf("\t\t\t\t\t\t\t\t<strong>IIR- BUTTERWORTH BANDPASS
clc; FILTER USING IMPULSE INVARIANT TRANSFORMATION </strong>\n");
Ap=input('Enter pass band ripple in dB,Ap : '); %Get the
fprintf("\t\t\t\t\t\t\t\t<strong>IIR- CHEBYSHEV BANDSTOP input
FILTER USING IMPULSE INVARIANT TRANSFORMATION</strong>\n"); As=input('Enter stop band attenuation in dB,As : ');
Ap=input('Enter pass band ripple in dB,Ap : '); %Get the Wpl=input('Enter lower passband edge frequency in rad : ');
input Wpu=input('Enter upper passband edge frequency in rad : ');
As=input('Enter stop band attenuation in dB,As : '); Wsl=input('Enter lower stopband edge frequency in rad : ');
Wpl=input('Enter lower passband edge frequency in rad : '); OUTPUT: Wsu=input('Enter upper stopband edge frequency in rad : ');
Wpu=input('Enter upper passband edge frequency in rad : '); Fs=input('Enter the sampling frequency in Hz,Fs : ');
Wsl=input('Enter lower stopband edge frequency in rad : ');
Wsu=input('Enter upper stopband edge frequency in rad : '); Wp=[Wpl Wpu]; %Vector of passband edge frequencies
Fs=input('Enter the sampling time period in Hz,Fs : '); Ws=[Wsl Wsu]; %Vector of stopband edge frequencies
Wp=[Wpl Wpu]; %Vector of passband edge frequencies omgp=(Wp*Fs); %Compute pre-warped analog frequency
Ws=[Wsl Wsu]; %Vector of stopband edge frequencies omgs=(Ws*Fs);
omgp=(Wp*Fs); %Compute pre-warped analog frequency %Butterworth IIR BPF Filter
omgs=(Ws*Fs); [N,Wn]=buttord(omgp,omgs,Ap,As,'s'); %Compute Cutoff edge
frequencies and Order
%Chebyshev IIR BSF Filter fprintf("\n");
[N,Wn] = cheb1ord(omgp,omgs,Ap,As,'s'); %Compute Cutoff edge disp("Butterworth IIR BPF Filter");
frequencies and Order fprintf("Order, N = ");disp(N);
fprintf("\n"); fprintf("Cutoff edge frequencies, Wn = ");disp(Wn);
disp("Chebyshev IIR BsF Filter");
fprintf("Order, N = ");disp(N); %Design a analog Butterworth BPF of Order N and Cutoff edge
fprintf("Cutoff edge frequencies, Wn = ");disp(Wn); frequencies Wn
[num,den]=butter(N,Wn,'bandpass','s');
%Design a analog Chebyshev BSF of Order N and Cutoff %Covert analog filter into digital using Impulse Invariant
frequency Wn Transformation
[num,den] = cheby1(N,Ap,Wn,'stop','s'); [B,A]=impinvar(num,den,Fs);
[B,A]=impinvar(num,den,Fs); W=0:0.01:pi;
%Covert analog filter into digital using Impulse Invariant [h,ph]=freqz(B,A,W,'whole'); %Obtain the frequency response
Transformation m=abs(h);
2. Design a FIR band stop filter for the given specification: p1 = 0.1 rad; s1 = 0.3 rad; M=ceil(((6.1*pi)/dW));
W=bartlett(M); subplot(2,2,3);
p2= 0.7 rad; s2 = 0.6 rad, Rp = 0.5 dB and As = 45dB. Choose the appropriate elseif(As<=45) plot(w1/pi,20*log(abs(h)),'r','LineWidth',0.75);
windowing technique as per the given stop band attenuation. Determine the disp('Hanning Window'); title("Magnitude Response of FIR-BSF");
M=ceil((6.2*pi)/dW); xlabel('Normalised Frequency');
impulse response and also plot the frequency response of the filter. W=hanning(M); ylabel('Gain in dB');
elseif(As<=53)
disp('Hamming Window'); subplot(2,2,4);
PROGRAM: M=ceil((6.6*pi)/dW); plot(w1/pi,angle(h),'m','LineWidth',0.75);
W=hamming(M); title("Angle Response of FIR-BSF");
elseif(As<=75) xlabel('Normalized Frequency');
%FIR-BANDSTOP FILTER USING WINDOW METHOD disp('Blackman Window'); ylabel('Phase');
close all; M=ceil((11*pi)/dW);
clc; W=blackman(M);
else OUTPUT:
fprintf("\t\t\t\t\t\t\t\t<strong>FIR-BANDSTOP FILTER disp('Please Enter a Correct Value');
USING WINDOW METHOD </strong>\n"); end
Ap=input('Enter pass band ripple in dB,Ap : '); %Get the
input %Compute Order of the filter
As=input('Enter stop band attenuation in dB,As : '); N=M-1;
Wpu=input('Enter upper passband edge frequency in rad : fprintf("Length of the filter: ");
'); disp(M);
Wpl=input('Enter lower passband edge frequency in rad : fprintf("Order of the filter: ");
'); disp(N);
Wsu=input('Enter upper stopband edge frequency in rad : if(mod(N,2)~= 0)
'); %Odd Order symmetric FIR filters must have a gain of zero
Wsl=input('Enter lower stopband edge frequency in rad : at Nyquist
'); %frequency. So, the order is increased by 1.
N=N+1;
end
dW= min((Wpu-Wsu),(Wsl-Wpl));%Compute Transition Width
fprintf("Transition Width: "); %Design a Bandstop FIR filter
disp(dW); B=fir1(N,[Wc1,Wc2],'stop');
Wc2=(Wpu+Wsu)/2; %Compute Cut-Off Frequency [h,w1]=freqz(B,M); %Determine x and y axis
Wc1=(Wpl+Wsl)/2;
fprintf("Cut-Off frequencies: "); %Plot the response graphs
disp(Wc1); subplot(2,2,1);
disp(Wc2); plot(B,'g','LineWidth',0.75);
title("Impulse Response of FIR-BSF");
%Compute Length of the Filter(M) and Window based on xlabel('Frequency');
Stopband Attenuation ylabel('Amplitude');
if(As<=21)
disp("Rectangular Window"); subplot(2,2,2);
M=ceil(((1.8*pi)/dW)); plot(W,'LineWidth',0.75);
W=rectwin(M); title("Window Response of FIR-BSF");
elseif(As<=25) xlabel('Frequency');
disp("Bartlett Window"); ylabel('Amplitude');
QUESTION: subplot(2,2,3); ii. Generate a sinusoidal signal with 1024 samples sampled at 4kHz. Decrease the
output = interp(input,L);
l=0:(L*N)-1; sample rate to 1kHz.
stem(l,output,'b');
i. Generate a sinusoidal signal with 500 samples sampled at 1kHz. Increase the hold on;
sample rate to 4kHz. plot(l,output,'r','LineWidth',0.75); PROGRAM
title("Output signal");
%MULTIRATE FILTERS
xlabel("n");
clc;
PROGRAM ylabel("Amplitude");
close all;
%MULTIRATE FILTERS %Plot the first 100 samples of Interploated Signal fprintf("\t\t\t\t\t\t\t\t\t\t\t\t<strong>MULTIRATE
clc; subplot(2,2,4); FILTERS</strong>\n");
close all; stem(l(1:L*100),output(1:L*100),'b'); %Get the input frequency
hold on;
fprintf("\t\t\t\t\t\t\t\t\t\t\t\t<strong>MULTIRATE plot(l(1:L*100),output(1:L*100),'r','LineWidth',0.75); f1= input("Enter the frequency of the Sinusoidal signal:
FILTERS</strong>\n"); title("Output signal - First L*100 samples"); ");
%Get the input frequency xlabel("n"); fs1=input("Enter the sampling frequency-1: ");
ylabel("Amplitude"); N=input("Enter the number of samples: ");
f1= input("Enter the frequency of the Sinusoidal signal: fs2=input("Enter the sampling frequency-2: ");
"); M= fs1/fs2;
fs1=input("Enter the sampling frequency-1: ");
N=input("Enter the number of samples: "); OUTPUT
n=(0:N-1); %Range N
fs2=input("Enter the sampling frequency-2: "); %Plot the input signal
L= fs2/fs1; subplot(2,2,1);
input=3*sin(2*pi*(f1/fs1)*n);
n=(0:N-1); %Range N stem(n,input,'b');
%Plot the input signal hold on;
subplot(2,2,1); plot(n,input,'r','LineWidth',0.75);
input=3*sin(2*pi*(f1/fs1)*n); title("Input signal");
stem(n,input,'b'); xlabel("n");
hold on; ylabel("Amplitude");
plot(n,input,'r','LineWidth',0.75);
title("Input signal"); %Plot the first 50 samples input signal
xlabel("n"); subplot(2,2,2);
ylabel("Amplitude"); stem(n(1:50),input(1:50),'b');
hold on;
%Plot the first 100 samples input signal plot(n(1:50),input(1:50),'r','LineWidth',0.75);
subplot(2,2,2); title("Input signal - First 50 samples");
stem(n(1:100),input(1:100),'b'); xlabel("n");
hold on; ylabel("Amplitude");
plot(n(1:100),input(1:100),'r','LineWidth',0.75);
title("Input signal - First 100 samples");
xlabel("n"); %Plot the Decimated Signal
ylabel("Amplitude"); subplot(2,2,3);
output = decimate(input,M,'fir');
m=0:(N/M)-1;
%Plot the Interploated Signal stem(m,output,'b');
hold on; iii. Generate a sinusoidal signal consisting of two frequency components 150Hz and %Plot the first 50 samples input signal
plot(m,output,'r','LineWidth',0.75); subplot(3,2,2);
title("Output signal"); 900Hz with 256 samples. Sample the signal at 3kHz. Increase the sample rate to stem(n(1:50),input(1:50),'b');
xlabel("n"); 12kHz and perform decimation of the interpolated signal to reduce the sampling hold on;
ylabel("Amplitude"); plot(n(1:50),input(1:50),'r','LineWidth',0.75);
rate to 3kHz. title("Input signal - First 50 samples");
%Plot the first 100 samples of Decimated Signal xlabel("n");
subplot(2,2,4); ylabel("Amplitude");
stem(m(1:(50/M)),output(1:(50/M)),'b'); PROGRAM
hold on;
plot(m(1:(50/M)),output(1:(50/M)),'r','LineWidth',0.75); %MULTIRATE FILTERS %Plot the Interploated Signal
title("Output signal - First 50/M samples"); clc; subplot(3,2,3);
xlabel("n"); close all; output = interp(input,L);
ylabel("Amplitude"); l=0:(L*N)-1;
fprintf("\t\t\t\t\t\t\t\t\t\t\t\t<strong>MULTIRATE stem(l,output,'b');
FILTERS</strong>\n"); hold on;
OUTPUT plot(l,output,'r','LineWidth',0.75);
%Get the input frequency title("Interpolated Output signal");
f1= input("Enter the frequency of the Sinusoidal signal- xlabel("n");
1: "); ylabel("Amplitude");
f2= input("Enter the frequency of the Sinusoidal signal-
2: "); %Plot the first 50 samples of Interploated Signal
fs1=input("Enter the sampling frequency-1: "); subplot(3,2,4);
N=input("Enter the number of samples: "); stem(l(1:L*50),output(1:L*50),'b');
fi=input("Enter the Interpolation frequency: "); hold on;
fd=input("Enter the Decimation frequency: "); plot(l(1:L*50),output(1:L*50),'r','LineWidth',0.75);
title("Interpolated Output signal - First L*50 samples");
%Decimation and Interpolation Factors xlabel("n");
L= fi/fs1; ylabel("Amplitude");
fprintf("Interpolation Factor, L: ");
disp(L);
M= fs1/fd; %Plot the Decimated Signal
fprintf("Decimation Factor, M: "); subplot(3,2,5);
disp(M); output = decimate(input,M,'fir');
m=0:(N/M)-1;
n=(0:N-1); %Range N stem(m,output,'b');
%Plot the input signal hold on;
subplot(3,2,1); plot(m,output,'r','LineWidth',0.75);
input=3*sin(2*pi*(f1/fs1)*n) + 3*sin(2*pi*(f2/fs1)*n); title("Decimated Output signal");
stem(n,input,'b'); xlabel("n");
hold on; ylabel("Amplitude");
plot(n,input,'r','LineWidth',0.75);
title("Input signal"); %Plot the first 50 samples of Decimated Signal
xlabel("n"); subplot(3,2,6);
ylabel("Amplitude"); stem(m(1:(50/M)),output(1:(50/M)),'b');
hold on;
plot(m(1:(50/M)),output(1:(50/M)),'r','LineWidth',0.75);
OUTPUT