0% found this document useful (0 votes)
16 views7 pages

Algorithm:: All "Enter The Input Sequence: " "Enter The Starting Point of The Sequence:"

The document outlines a MATLAB program for generating and manipulating various signals, including discrete and continuous time signals such as sinusoidal, exponential, and impulse signals. It also covers signal processing techniques like autocorrelation and cross-correlation, along with random signal generation using normal and uniform distributions. Additionally, the document provides algorithms for signal shifting, addition, amplification, and linear convolution.

Uploaded by

mohith.r0101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views7 pages

Algorithm:: All "Enter The Input Sequence: " "Enter The Starting Point of The Sequence:"

The document outlines a MATLAB program for generating and manipulating various signals, including discrete and continuous time signals such as sinusoidal, exponential, and impulse signals. It also covers signal processing techniques like autocorrelation and cross-correlation, along with random signal generation using normal and uniform distributions. Additionally, the document provides algorithms for signal shifting, addition, amplification, and linear convolution.

Uploaded by

mohith.r0101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

ALGORITHM: y_axis= A*exp(x_axis); ii) Generate all the signals in continuous - time domain.

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);

title('Continuous Time Sinusoidal Signal'); PROGRAM: RANDOM SIGNAL/SEQUENCE SIGNAL MANIPULATION


xlabel('T');
ylabel('Amplitude'); Random number generators are useful in signal processing for testing and evaluating various
Some of the basic signal manipulation may be listed as follows:
signal processing algorithms. For example, in order to simulate a particular noise cancellation
N=input("\nContinuous Time Cosine Signal\n Enter the Time period T: ");
A=input(" Enter the Amplitude: "); algorithm (technique), we need to create some signals which is contaminated by noise and then i) Signal Shifting / Delay
f=input(" Enter the frequency: ");
x_axis=(0:0.001:N); apply this signal to the algorithm and monitor its output. The input/ output spectrum may then be Here 'D' is the number of samples which the input signal must be delayed. For
y_axis= A*cos(2*pi*f*x_axis); used to examine and measure the performance of noise canceller algorithm. Random numbers are example, if x(n) = [1, 2, 3, 4, 5], then x(n-3) = [0,0,0,1,2,3,4,5].
subplot(3,2,6);
plot(x_axis,y_axis); generated to represent the samples of some noise signal which is then added to the samples of
title('Continuous Time Cosine Signal'); ii) Signal Addition / Subtraction
some the wanted signal to create an overall noisy signal.
xlabel('T');
When adding two signals together, signals must have the same number of samples.
ylabel('Amplitude'); MATLAB provides two commands, which may be applied to generate random numbers.
If one signal has less than number of samples than the other, then this signal may be
appended with zeros in order to make it equal length to the second signal before adding
Normally Distributed Random Numbers:
OUTPUT: them.
randn(N)
Is an N-by-N matrix with random entries, chosen from a normal distribution with mean zero iii) Signal Amplification / Attenuation
and variance one. 'a' is a numerical constant. If a>1, then the process is referred to as 'amplification’, if
randn(M,N), and randn([M,N]) are M-by-N matrices with random entries. 0<a<1, the process is referred to as 'attenuation'.
Uniformly Distributed Random Numbers:
rand(N) is an N-by-N matrix with random entries, chosen from a uniform distribution on the A MATLAB program given below, provides an example of each of the above basic operations:
interval (0.0,1.0).
rand(M,N) and rand([M,N]) are M-by-N matrices with random entries. Write a program to generate three sinusoidal signals of 128 samples with input frequencies 150
Hz, 450 Hz, 1500Hz. Sample the three sinusoidal signals with a sampling frequency of 8000 Hz.
The following MATLAB program generates random signals using each distribution. i) Generate and Plot all the sinusoidal signals.
PROGRAM: ii) Delay the first sinusoidal signal by 20 samples.
clc; iii) Advance the second sinusoidal signal by 10 samples.
close all;
iv) Add the first and second sinusoidal signal.
n=input("Random Signals\n Enter the number of Samples: ");
v) Multiply the first and third sinusoidal signal.
y1=rand(1,n);
y2=randn(1,n);
plot(y1);
hold on; PROGRAM:
plot(y2,"ro"); clc;
title("Random Signals");
close all;
xlabel("n");
ylabel("Amplitude");
legend('Uniform Distributed Signal','Standard Normal Distributed Signal'); disp("Signal Manipulation");
N=input(" Enter the Total number of Samples: ");
The following is the output of the above program: f1=input(" Enter the frequency of First signal: ");

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:

• Get two signals x(m)and h(p)in matrix form


PROGRAM: • The correlated signal is denoted as y(n) PROGRAM:
clc; • y(n)is given by the formula 𝑦𝑦(𝑛𝑛) = ∑∞ clc;
𝑘𝑘=−∞ 𝑥𝑥(𝑘𝑘)ℎ(𝑘𝑘 − 𝑛𝑛)
close all; close all;
• Stop
disp("Autocorrelation of a Sinusoidal Signal"); fprintf("Cross Corelation of two sinusoidal
fi=input("Enter the frequency of the signal: "); I) COMPUTING CROSS- CORRELATION OF THE GIVEN INPUT SEQUENCE: signals\n");
fs=input("Enter the sampling frequency: "); II) f1=input("Enter the frequency of Signal-1: ");
N=input("Enter the number of samples: "); f2=input("Enter the frequency of Signal-2: ");
PROGRAM: fs=input("Enter the sampling frequency: ");
t=(0:0.01:7); N=input("Enter the number of samples: ");
clc;
subplot(3,1,1);
close all;
y1=sin(2*pi*fi*t); n=(0:N-1);
plot(t,y1); fprintf("Cross Corelation of two sequences\n"); subplot(3,1,1);
title("Input signal"); X1=input("Enter the input sequence-1: "); x=sin(2*pi*(f1/fs)*n);
xlabel("t"); spx1=input("Enter the starting point of the plot(n,x);
ylabel("y(t)"); sequence:"); title("Signal-1");
X2=input("Enter the input sequence-2: "); xlabel("n");
n=(0:N-1); ylabel("X[n]");
spx2=input("Enter the starting point of the
subplot(3,1,2);
sequence:");
y2=sin(2*pi*(fi/fs)*n); subplot(3,1,2);
len1=length(X1);
plot(n,y2); y=sin(2*pi*(f2/fs)*n);
len2=length(X2);
title("Sampled signal"); plot(n,y);
xlabel("n"); subplot(3,1,1); title("Signal-2");
ylabel("y[n]"); stem(spx1:spx1+len1-1,X1,'fill','r'); xlabel("n");
title("Sequence-1"); ylabel("Y[n]");
subplot(3,1,3);
xlabel("n");
[autocorr,lags]=xcorr(y2); [corr,lags]=xcorr(x,y);
ylabel("X[n]");
plot(lags,autocorr); subplot(3,1,3);
title("Autocorrelation of the signal"); subplot(3,1,2); plot(lags,corr);
xlabel("n"); stem(spx2:spx2+len2-1,X2,'fill','g'); title("Cross Correlation of the signals");
ylabel("Y[n]"); title("Sequence-2"); xlabel("n");
xlabel("n"); ylabel("Z[n]");
ylabel("Y[n]");
OUTPUT:
[autocorr,lags]=xcorr(X1,X2); OUTPUT:
lags
autocorr
subplot(3,1,3);
stem(lags,autocorr,'fill','b');
title("Cross Correlation of the sequences");
xlabel("n");
ylabel("Z[n]");

OUTPUT:

PROGRAM: LINEAR CONVOLUTION USING FOR LOOP: for i = 1 : lx+lh-1


for j = 1 : i
clc; ALGORITHM: y(i) = y (i)+(X(j)*H(i-j+1));
close all; end
• Start end
x=input("ENTER THE INPUT SEQUENCE 1=");
spx=input("ENTER THE STARTING POINT OF SEQUENCE 1="); • Read the first sequence subplot(1,3,3);
stem(spx+sph:spx+sph+ly-1, y, 'fill','g');
lx=length(x); • Read the second sequence xlabel('sample');
subplot(1,3,1); ylabel('Amplitude');
stem(spx:spx+lx-1,x,'fill','r'); • Find the length of first sequence title('LINEAR CONVOLUTION USING FOR LOOP');
xlabel('sample');
ylabel('Amplitude'); • Find the length of second sequence
title('INPUT SEQUENCE 1'); • Perform linear convolution for both the sequences
OUTPUT:
h=input("ENTER THE INPUT SEQUENCE 2="); • Plot the sequence
sph=input("ENTER THE STARTING POINT OF SEQUENCE 2=");
lh=length(h); • Display the output sequence
subplot(1,3,2); • Stop
stem(sph:sph+lh-1,h,'fill', 'g');
xlabel('sample');
ylabel('Amplitude'); PROGRAM:
title('INPUT SEQUENCE 2');
close all;
y=conv(x,h); clc;
ly=length(y);
subplot(1,3,3); x=input("ENTER THE INPUT SEQUENCE 1=");
stem(spx+sph:spx+sph+ly-1,y); spx=input("ENTER THE STARTING POINT OF SEQUENCE 1=");
xlabel('sample'); lx=length(x);
ylabel('Amplitude'); subplot(1,3,1);
title('LINEAR CONVOLUTION'); stem(spx:spx+lx-1,x,'fill','b');
xlabel('sample');
ylabel('Amplitude');
title('INPUT SEQUENCE 1');
OUTPUT:
h=input("ENTER THE INPUT SEQUENCE 2=");
sph=input("ENTER THE STARTING POINT OF SEQUENCE 2=");
lh=length(h);
subplot(1,3,2);
stem(sph:sph+lh-1,h,'fill', 'r');
xlabel('sample');
ylabel('Amplitude');
title('INPUT SEQUENCE 2');

ly=lx+lh-1;
X= [x, zeros(1,(ly-lx))];
H = [h, zeros(1,(ly-lh))];
y= zeros(1, lx+lh-1);

Circular convolution using inbuilt command


ALGORITHM: Circular convolution using for loop circfor=zeros(1,ly);
for i = 1 : ly
• Start the program ALGORITHM: for j = 1 : i
circfor(i) = circfor(i)+(X(j)*H(i-j+1));
• Enter the first input sequence x(n). • Start end
• Enter the second input sequence h(n) • Read the first sequence end
subplot(1,3,3);
• Plot both the sequences in discrete form using stem function • Read the second sequence stem(spx+sph:spx+sph+ly-1, circfor, 'fill','b');
xlabel('sample');
• Use the inbuilt command cconv( ) function to perform the circular convolution • Find the length of first sequence ylabel('Amplitude');
operation • Find the length of second sequence title('CIRCULAR CONVOLUTION USING fOR LOOP');

• 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);

signal=cos(2*pi*(f1/fs)*n)+cos(2*pi*(f2/fs)*n)+cos(2*pi*(f3/f ANALYSIS BY FFT: subplot(2,2,3);


s)*n); fs=2*f3;
[y,f]=pspectrum(signal,fs); PROGRAM: signal=cos(2*pi*(f1/fs)*n)+cos(2*pi*(f2/fs)*n)+cos(2*pi*(f3/f
plot(f,y); clc; s)*n);
title("Power Density - Nyquist sampled frequency "); close all; signal=fft(signal);
xlabel("Frequency(Hz)"); val_signal=abs(signal);
ylabel("Power"); fprintf("\t\t\t\t\t\t\t\t\t\t\t\t"+"Sampling and Effect of n1=0:(N/2)-1;
Aliasing\n"); f=(n1*fs)/N;
%Plot the underdamped signal %Get the input frequency signal_plot=20*log10(val_signal(1:N/2));
subplot(2,2,2); f1=input("Enter the frequency of the Cosine signal -1: "); plot(f,signal_plot);
n=(0:N-1); f2=input("Enter the frequency of the Cosine signal -2: "); title("FFT - Nyquist Sampled Signal");
fs=f3/1.5; f3=input("Enter the frequency of the Cosine signal -3: "); xlabel("Frequency(Hz)");
signal=cos(2*pi*(f1/fs)*n)+cos(2*pi*(f2/fs)*n)+cos(2*pi*(f3/f fs=input("Enter the sampling frequency: "); ylabel("Power");
s)*n); N=input("Enter the number of samples: ");
[y,f]=pspectrum(signal,fs); %Oversampled signal
plot(f,y); %Plot the real signal fs=10kHz subplot(2,2,4);
title("Power Density - Undersampled Signal"); subplot(2,2,1); fs=4*f3;
xlabel("Frequency(Hz)"); n=(0:N-1); signal=cos(2*pi*(f1/fs)*n)+cos(2*pi*(f2/fs)*n)+cos(2*pi*(f3/f
ylabel("Power"); signal=cos(2*pi*(f1/fs)*n)+cos(2*pi*(f2/fs)*n)+cos(2*pi*(f3/f s)*n);
s)*n); signal=fft(signal);
%Plot the Overdamped signal signal=fft(signal); val_signal=abs(signal);
subplot(2,2,4); val_signal=abs(signal); n1=0:(N/2)-1;
n=(0:N-1); n1=0:(N/2)-1; f=(n1*fs)/N;
fs=4*f3; f=(n1*fs)/N; signal_plot=20*log10(val_signal(1:N/2));
signal=cos(2*pi*(f1/fs)*n)+cos(2*pi*(f2/fs)*n)+cos(2*pi*(f3/f signal_plot=20*log10(val_signal(1:N/2)); plot(f,signal_plot);
s)*n); plot(f,signal_plot); title("Oversampled Signal");
[y,f]=pspectrum(signal,fs); title("FFT - Sampling frequency = 10kHz"); xlabel("Frequency(Hz)");
plot(f,y); xlabel("Frequency(Hz)"); ylabel("Power");
title("Power Density - Oversampled Signal"); ylabel("Power");
xlabel("Frequency(Hz)");
ylabel("Power"); OUTPUT:
%Undersampled signal
subplot(2,2,2);
OUTPUT: n=(0:N-1);
fs=f3/1.5;
signal=cos(2*pi*(f1/fs)*n)+cos(2*pi*(f2/fs)*n)+cos(2*pi*(f3/f
s)*n);
signal=fft(signal);
val_signal=abs(signal);
n1=0:(N/2)-1;
f=(n1*fs)/N;
signal_plot=20*log10(val_signal(1:N/2));
plot(f,signal_plot);
title("FFT - Undersampled Signal");
xlabel("Frequency(Hz)");
ylabel("Power");

%Nyquist sampled signal


Parseval’s Theorem title('Input - Real Sequence'); fprintf('<strong>IDFT of Input Sequence</strong>\n');
xlabel('n'); disp(idft);
For complex valued sequences x(n) and y(n), in general ylabel('Value'); subplot(3,3,7);
If, stem(t,idft);
x(n) X(K) x_imag = imag(x); %Display the imaginary value of the input title('IDFT Sequence');
sequence xlabel('n');
fprintf('Imaginery Value of Input Sequence: '); ylabel('Value');
Then,
disp(x_imag);
|x(n)| = |X(K)| subplot(3,3,3); idft_real = real(idft); %Display the real value of the IDFT
stem(t,x_imag); %Plot the imaginary part of the given sequence
sequence fprintf('Real Value of IDFT: ');
QUESTION: title('Input - Imaginary Sequence'); disp(idft_real);
xlabel('n'); subplot(3,3,8);
ylabel('Value'); stem(t,idft_real);
Compute 8-point DFT of the given sequence x[n] = [1+i, 1+i, 1+i, 1+i, 0, 0, 0, 0] title('IDFT Real Sequence');
dft=fft(x,N); %Compute DFT of the sequence using FFT xlabel('n');
fprintf('<strong>DFT of Input Sequence:</strong>\n'); ylabel('Value');
PROGRAM: disp(dft);
subplot(3,3,4); idft_imag = imag(idft); %Display the imaginary value of the
close all; stem(t,dft,'red'); %Plot the DFT of the given sequence IDFT sequence
clc; title('DFT'); fprintf('Imaginery Value of IDFT: ');
xlabel('n'); disp(idft_imag);
fprintf("\t\t\t\t\t\t\t\t\t<strong>Computation of 8- point ylabel('Value'); subplot(3,3,9);
DFT</strong>\n"); stem(t,idft_imag,'green');
x=input('Enter the input sequence x[n] = '); %Get the input mag=abs(dft); %Compute the magnitude of the DFT sequence title('IDFT Imaginery Sequence');
sequence fprintf('Magnitude of the DFT Sequence: '); xlabel('n');
disp(mag); ylabel('Value')
N=length(x); % Display the length of the sequence subplot(3,3,5);
fprintf('\n<strong>Length of the Input Sequence:</strong> '); stem(t,mag,'green'); % Plot the magnitude sequence
disp(N); title('Magnitude Sequence'); OUTPUT:
t=0:N-1; xlabel('n');
ylabel('Magnitude');
subplot(3,3,1);%Plot the input sequence
fprintf("<strong>Input sequence:</strong>\n"); phase=angle(dft); %Compute the phase of the DFT sequence
disp(x); disp('Phase angle of the DFT Sequence');
stem(t,x,'red'); %Plot the Input sequence of the given fprintf('\tPhase Angle in Radians: ');
sequence disp(phase);
title('Input Sequence'); phaseang=rad2deg(phase); %Coversion of radians to degree
xlabel('n'); fprintf('\tPhase Angle in Degree: ');
ylabel('Value'); disp(phaseang);
subplot(3,3,6); %Plot the phase angle sequence
x_real = real(x); %Display the real value of the input stem(t,phaseang,'red');
sequence title('Phase Angle Sequence');
fprintf("Real Value of Input Sequence: "); xlabel('n');
disp(x_real); ylabel('Degrees');
subplot(3,3,2);
stem(t,x_real,'red'); %Plot the real part of the given idft=ifft(dft); %Compute the Inverse DFT
sequence

VERIFICATION OF LINEARITY: VERIFICATION OF PARSEVAL’S THEOREM: ALGORITHM:


PROGRAM: PROGRAM: 1. Get the passband and stopband ripples
close all; close all; 2. Get the passband and stopband edge frequencies
clc; clc;
fprintf("\t\t\t\t\t\t\t\t\t<strong>VERIFICATION OF LINEARITY 3.
PROPERTY OF DFT</strong>\n"); fprintf("\t\t\t\t\t\t\t\t\t<strong>VERIFICATION OF PARSEVAL'S 4. Calculate the corresponding analog frequency
disp("Linearity"); THEOREM OF DFT</strong>\n");
fprintf("\t<strong>DFT(ax1(n)+bx2(n))= disp("PARSEVAL'S THEOREM"); 5. Calculate the order and cut-off frequency of the filter using Butterworth or
aX1(\x3C9)+bX2(\x3C9)</strong>, where\n"); fprintf("\t<strong>\x2211|x[n]|^2 =(1/N)
Chebyshev as per the specification
fprintf("\t\ta and b are constants\n\t\tx1(n) and x(2) are \x2211|X(K)|^2</strong>, where\n");
sequences"); fprintf("\n\t\tx[n] is the sequence and X(K)is the DFT of 6. Design the analog counterpart.
fprintf("\n\t\tX1(\x3C9)and X2(\x3C9) are DFT of x1(n) and x[n]respectively\n\n");
x(2) respectively\n\n"); 7.
x1=input('Enter the First input sequence x1[n] = '); %Get x=input('Enter the input sequence x[n] = '); 8.
the two input sequences N=length(x);
x2=input('Enter the Second input sequence x2[n] = '); fprintf('Length of the Input Sequence: ');
disp(N);
N1=length(x1); %Display the length of the sequences y=fft(x,N); QUESTION:
fprintf('Length of the First Input Sequence: '); disp('DFT of the Input Sequence'); 1. To design an IIR Butterworth and Chebyshev LPF for the following specification:
disp(N1); disp(y);
N2=length(x2); LHS=sum(abs(x).^2); Ap
fprintf('Length of the Second Input Sequence: '); disp('Displaying LHS');
disp(N2); disp(LHS); As
a1=input('Enter the first coefficient a1='); RHS=sum(abs(y).^2)/N;
a2=input('Enter the second coefficient a2='); %Get the co- disp('Displaying RHS');
efficients disp(RHS); PROGRAM:

a=a1*x1 + a2*x2; if(LHS==RHS)


LHS=fft(a); fprintf('LHS=RHS\n Hence Parsevals Theorem is %IIR- BUTTERWORTH AND CHEBYSHEV LOWPASS FILTERS USING
disp('Displaying LHS'); Verified\n'); BILINEAR TRANSFORMATION
disp(LHS); else close all;
disp('Parsevals Theorem is not Proved'); clc;
X1=fft(x1,N1); end
X2=fft(x2,N2); fprintf("\t\t\t\t\t\t\t\t\t<strong>IIR- BUTTERWORTH AND
Y1=a1*X1; CHEBYSHEV LOWPASS FILTERS USING BILINEAR TRANSFORMATION
Y2=a2*X2; </strong>\n");
Y=Y1+Y2; OUTPUT: Ap=input('Enter pass band ripple in dB,Ap : ');
RHS=Y; As=input('Enter stop band attenuation in dB,As : ');
disp('Displaying RHS'); Wp=input('Enter pass band edge frequency in rad, Wp : ');
disp(RHS); Ws=input('Enter stop band edge frequency in rad, Ws : ');
Ts=input('Enter the sampling time period in s,Ts : ');
if(LHS==RHS)
fprintf('LHS=RHS\nHence,Linearity is Proved\n'); omgp=(2/Ts)*tan(Wp/2); %Compute pre-warped analog
else frequency
disp('Linearity not proved'); omgs=(2/Ts)*tan(Ws/2);
end

%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);

%Design a analog Butterworth BPF of Order N and Cutoff edge


frequencies Wn
[num,den]=butter(N,Wn,'bandpass','s');

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);

an=angle(h); 2. CHEBYSHEV BSF USING BILINEAR TRANSFORMATION METHOD xlabel('Normalised frequency');


ylabel('Gain in dB');
subplot(2,1,1); %Plot the response graphs subplot(2,1,2);
plot(ph/pi,20*log(m));
PROGRAM:
plot(ph/pi,an);
title('Butterworth IIR BPF Filter - Magnitude Response'); title('Chebyshev IIR BSF Filter - Phase Response');
xlabel('Normalised frequency'); %IIR- CHEBYSHEV BANDSTOP FILTER USING BILINEAR TRANSFORMATION xlabel('Normalised frequency');
ylabel('Gain in dB'); close all; ylabel('Angle in Radians');
subplot(2,1,2); clc;
plot(ph/pi,an);
title('Butterworth IIR BPF Filter - Phase Response'); fprintf("\t\t\t\t\t\t\t\t<strong>IIR- CHEBYSHEV BANDSTOP
FILTER USING BILINEAR TRANSFORMATION</strong>\n"); OUTPUT:
xlabel('Normalised frequency');
ylabel('Angle in Radians'); Ap=input('Enter pass band ripple in dB,Ap : '); %Get the
input
As=input('Enter stop band attenuation in dB,As : ');
Wpl=input('Enter lower passband edge frequency in rad : ');
OUTPUT: Wpu=input('Enter upper passband edge frequency in rad : ');
Wsl=input('Enter lower stopband edge frequency in rad : ');
Wsu=input('Enter upper stopband edge frequency in rad : ');
Fs=input('Enter the sampling time period in Hz,Fs : ');

Wp=[Wpl Wpu]; %Vector of passband edge frequencies


Ws=[Wsl Wsu]; %Vector of stopband edge frequencies

omgp=(2*Fs)*tan(Wp/2); %Compute pre-warped analog frequency


omgs=(2*Fs)*tan(Ws/2);

%Chebyshev IIR BSF Filter


[N,Wn] = cheb1ord(omgp,omgs,Ap,As,'s'); %Compute Cutoff edge
frequencies and Order
fprintf("\n");
disp("Chebyshev IIR BsF Filter");
fprintf("Order, N = ");disp(N);
fprintf("Cutoff edge frequencies, Wn = ");disp(Wn);

%Design a analog Chebyshev BSF of Order N and Cutoff


frequency Wn
[num,den] = cheby1(N,Ap,Wn,'stop','s');
[B,A]=bilinear(num,den,Fs);
%Covert analog filter into digital using Bilinear
Transformation
W=0:0.01:pi;
[h,ph]=freqz(B,A,W,'whole'); %Obtain the frequency response
m=abs(h);
an=angle(h);

subplot(2,1,1); %Plot the response graphs


plot(ph/pi,20*log(m));
title('Chebyshev IIR BSF Filter - Magnitude Response');
LOW PASS FIR M=ceil((11*pi)/dW); HIGH PASS FIR
W=blackman(M);
else
PROGRAM: disp('Please Enter a Correct Value'); PROGRAM:
end
%FIR-LOW PASS FILTER USING WINDOW METHOD %FIR-HIGH PASS FILTER USING WINDOW METHOD
%Compute Order of the filter close all;
close all;
N=M-1; clc;
clc;
fprintf("Length of the filter: ");
disp(M); fprintf("\t\t\t\t\t\t\t\t<strong>FIR-HIGH PASS FILTER USING
fprintf("\t\t\t\t\t\t\t\t<strong>FIR-LOW PASS FILTER
fprintf("Order of the filter: "); WINDOW METHOD </strong>\n");
USING WINDOW METHOD </strong>\n");
disp(N); Ap=input('Enter pass band ripple in dB,Ap : '); %Get the
Ap=input('Enter pass band ripple in dB,Ap : '); %Get input
the input As=input('Enter stop band attenuation in dB,As : ');
%Design a Lowpass FIR filter
As=input('Enter stop band attenuation in dB,As : '); Wp=input('Enter passband edge frequency in rad : ');
B=fir1(N,Wc,'low');
Wp=input('Enter passband edge frequency in rad : '); Ws=input('Enter stopband edge frequency in rad : ');
[h,w1]=freqz(B,M); %Determine x and y axis
Ws=input('Enter stopband edge frequency in rad : ');
dW= Wp-Ws;%Compute Transition Width
%Plot the response graphs fprintf("Transition Width: ");
dW= Ws-Wp;%Compute Transition Width
subplot(2,2,1); disp(dW);
fprintf("Transition Width: ");
plot(B,'g','LineWidth',0.75); Wc=(Wp+Ws)/2; %Compute Cut-Off Frequency
disp(dW);
title("Impulse Response of FIR-LPF"); fprintf("Cut-Off frequency: ");
Wc=(Wp+Ws)/2; %Compute Cut-Off Frequency
xlabel('Frequency'); disp(Wc);
fprintf("Cut-Off frequency: ");
ylabel('Amplitude');
disp(Wc); %Compute Length of the Filter(M) and Window based on Stopband
subplot(2,2,2);
plot(W,'LineWidth',0.75); Attenuation
%Compute Length of the Filter(M) and Window based on if(As<=21)
title("Window Response of FIR-LPF");
Stopband Attenuation disp("Rectangular Window");
xlabel('Frequency');
if(As<=21) M=ceil(((1.8*pi)/dW));
ylabel('Amplitude');
disp("Rectangular Window"); W=rectwin(M);
M=ceil(((1.8*pi)/dW)); elseif(As<=25)
subplot(2,2,3); disp("Bartlett Window");
W=rectwin(M);
plot(w1/pi,20*log(abs(h)),'r','LineWidth',0.75); M=ceil(((6.1*pi)/dW));
elseif(As<=25)
title("Magnitude Response of FIR-LPF"); W=bartlett(M);
disp("Bartlett Window");
xlabel('Normalised Frequency'); elseif(As<=45)
M=ceil(((6.1*pi)/dW));
ylabel('Gain in dB'); disp('Hanning Window');
W=bartlett(M);
M=ceil((6.2*pi)/dW);
elseif(As<=45)
subplot(2,2,4); W=hanning(M);
disp('Hanning Window'); elseif(As<=53)
plot(w1/pi,angle(h),'m','LineWidth',0.75);
M=ceil((6.2*pi)/dW); disp('Hamming Window');
title("Angle Response of FIR-LPF");
W=hanning(M); M=ceil((6.6*pi)/dW);
xlabel('Normalized Frequency');
elseif(As<=53) W=hamming(M);
ylabel('Phase');
disp('Hamming Window'); elseif(As<=75)
M=ceil((6.6*pi)/dW); disp('Blackman Window');
W=hamming(M); M=ceil((11*pi)/dW);
elseif(As<=75) W=blackman(M);
disp('Blackman Window'); else

disp('Please Enter a Correct Value'); PROGRAM: M=ceil((11*pi)/dW);


end W=blackman(M);
else
%Compute Order of the filter %FIR-BANDPASS FILTER USING WINDOW METHOD disp('Please Enter a Correct Value');
N=M-1; close all; end
fprintf("Length of the filter: "); clc;
disp(M); %Compute Order of the filter
fprintf("Order of the filter: "); fprintf("\t\t\t\t\t\t\t\t<strong>FIR-BANDPASS FILTER USING N=M-1;
disp(N); WINDOW METHOD </strong>\n"); fprintf("Length of the filter: ");
if(mod(N,2)~= 0) Ap=input('Enter pass band ripple in dB,Ap : '); %Get the disp(M);
%Odd Order symmetric FIR filters must have a gain of zero at input fprintf("Order of the filter: ");
Nyquist As=input('Enter stop band attenuation in dB,As : '); disp(N);
%frequency. So, the order is increased by 1. Wpu=input('Enter upper passband edge frequency in rad : '); if(mod(N,2)~= 0)
N=N+1; Wpl=input('Enter lower passband edge frequency in rad : '); %Odd Order symmetric FIR filters must have a gain of zero at
end Wsu=input('Enter upper stopband edge frequency in rad : '); Nyquist
Wsl=input('Enter lower stopband edge frequency in rad : '); %frequency. So, the order is increased by 1.
%Design a High pass FIR filter N=N+1;
B=fir1(N,Wc,'high'); end
[h,w1]=freqz(B,M); %Determine x and y axis dW= min((Wsu-Wpu),(Wpl-Wsl));%Compute Transition Width
fprintf("Transition Width: "); %Design a Bandpass FIR filter
%Plot the response graphs B=fir1(N,[Wc1,Wc2],'bandpass');
disp(dW);
subplot(2,2,1); [h,w1]=freqz(B,M); %Determine x and y axis
Wc2=(Wpu+Wsu)/2; %Compute Cut-Off Frequency
plot(B,'g','LineWidth',0.75);
Wc1=(Wpl+Wsl)/2;
title("Impulse Response of FIR-HPF"); %Plot the response graphs
fprintf("Cut-Off frequencies: ");
xlabel('Frequency'); subplot(2,2,1);
disp(Wc1);
ylabel('Amplitude'); plot(B,'g','LineWidth',0.75);
disp(Wc2);
title("Impulse Response of FIR-BPF");
subplot(2,2,2); xlabel('Frequency');
plot(W,'LineWidth',0.75); %Compute Length of the Filter(M) and Window based on Stopband
Attenuation ylabel('Amplitude');
title("Window Response of FIR-HPF");
xlabel('Frequency'); if(As<=21)
disp("Rectangular Window"); subplot(2,2,2);
ylabel('Amplitude'); plot(W,'LineWidth',0.75);
M=ceil(((1.8*pi)/dW));
W=rectwin(M); title("Window Response of FIR-BPF");
subplot(2,2,3); xlabel('Frequency');
plot(w1/pi,20*log(abs(h)),'r','LineWidth',0.75); elseif(As<=25)
disp("Bartlett Window"); ylabel('Amplitude');
title("Magnitude Response of FIR-HPF");
xlabel('Normalised Frequency'); M=ceil(((6.1*pi)/dW));
W=bartlett(M); subplot(2,2,3);
ylabel('Gain in dB'); plot(w1/pi,20*log(abs(h)),'r','LineWidth',0.75);
elseif(As<=45)
disp('Hanning Window'); title("Magnitude Response of FIR-BPF");
subplot(2,2,4);
M=ceil((6.2*pi)/dW); xlabel('Normalised Frequency');
plot(w1/pi,angle(h),'m','LineWidth',0.75);
W=hanning(M); ylabel('Gain in dB');
title("Angle Response of FIR-HPF");
xlabel('Normalized Frequency'); elseif(As<=53)
disp('Hamming Window'); subplot(2,2,4);
ylabel('Phase');
M=ceil((6.6*pi)/dW); plot(w1/pi,angle(h),'m','LineWidth',0.75);
W=hamming(M); title("Angle Response of FIR-BPF");
elseif(As<=75) xlabel('Normalized Frequency');
disp('Blackman Window'); ylabel('Phase');

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);

title("Decimated Output signal - First 50/M samples");


xlabel("n");
ylabel("Amplitude");

OUTPUT

You might also like