0% found this document useful (0 votes)
18 views4 pages

Bit 1

The document outlines a MATLAB program for generating and manipulating various signals, including continuous and discrete time sinusoidal, exponential, impulse, step, and ramp signals. It also describes signal manipulation techniques such as shifting, addition, and amplification, as well as the generation of random signals. Additionally, it includes algorithms for computing autocorrelation and cross-correlation of signals, along with examples of linear and circular 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)
18 views4 pages

Bit 1

The document outlines a MATLAB program for generating and manipulating various signals, including continuous and discrete time sinusoidal, exponential, impulse, step, and ramp signals. It also describes signal manipulation techniques such as shifting, addition, and amplification, as well as the generation of random signals. Additionally, it includes algorithms for computing autocorrelation and cross-correlation of signals, along with examples of linear and circular 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/ 4

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

title('Continuous Time Sinusoidal Signal');


subplot(3,2,4); xlabel('T');
• Start the MATLAB software stem(x_axis,y_axis); PROGRAM: ylabel('Amplitude');
title('Discrete Time Exponential Signal'); clc;
• Assign the time and amplitude values xlabel('n'); close all; N=input("\nContinuous Time Cosine Signal\n Enter the Time period T: ");
• Use the necessary commands to generate the signal ylabel('Amplitude'); A=input(" Enter the Amplitude: ");
range=input("Unit Impulse Signal\n Enter the range of X-axis: "); f=input(" Enter the frequency: ");
• 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); x_axis=(0:0.001:N);
A=input(" Enter the Amplitude: "); y_axis= A*cos(2*pi*f*x_axis);
• 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); subplot(3,2,6);
x_axis=(0:0.1:N); plot(x_axis,y_axis); plot(x_axis,y_axis);
y_axis= A*sin(2*pi*f*x_axis); title('Unit Impulse Signal'); title('Continuous Time Cosine Signal');
PROGRAM: GENERATION OF SEQUENCE (OR) SIGNAL subplot(3,2,5); xlabel('t'); xlabel('T');
i) Generate all the signals in discrete - time domain. stem(x_axis,y_axis); ylabel('Amplitude'); 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)]; OUTPUT:
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);

PROGRAM: RANDOM SIGNAL/SEQUENCE SIGNAL MANIPULATION f2=input(" Enter the frequency of Second signal: "); y4= y1+y2;
f3=input(" Enter the frequency of Third signal: "); subplot(4,2,6);
Random number generators are useful in signal processing for testing and evaluating various
Some of the basic signal manipulation may be listed as follows: fs=input(" Enter the Sampling frequency: "); plot(n,y4);
signal processing algorithms. For example, in order to simulate a particular noise cancellation title("Signal-1 + Signal-2 ");
algorithm (technique), we need to create some signals which is contaminated by noise and then n=(0:N-1); xlabel("n");
i) Signal Shifting / Delay
y1=sin(2*pi*(f1/fs)*n); ylabel("Amplitude");
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 y2=sin(2*pi*(f2/fs)*n);
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]. y3=sin(2*pi*(f3/fs)*n); y5=y1.*y3;
subplot(4,2,7);
generated to represent the samples of some noise signal which is then added to the samples of
ii) Signal Addition / Subtraction subplot(4,2,1); plot(n,y5);
some the wanted signal to create an overall noisy signal. plot(n,y1); title("Signal-1 x Signal-3");
When adding two signals together, signals must have the same number of samples. title("Signal-1"); xlabel("n");
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 xlabel("n"); ylabel("Amplitude");
ylabel("Amplitude");
appended with zeros in order to make it equal length to the second signal before adding
Normally Distributed Random Numbers:
them. subplot(4,2,3);
randn(N) OUTPUT:
plot(n,y2);
Is an N-by-N matrix with random entries, chosen from a normal distribution with mean zero iii) Signal Amplification / Attenuation title("Signal-2");
and variance one. xlabel("n");
'a' is a numerical constant. If a>1, then the process is referred to as 'amplification’, if
ylabel("Amplitude");
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: subplot(4,2,5);
rand(N) is an N-by-N matrix with random entries, chosen from a uniform distribution on the plot(n,y3);
A MATLAB program given below, provides an example of each of the above basic operations:
title("Signal-3");
interval (0.0,1.0). xlabel("n");
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 ylabel("Amplitude");

Hz, 450 Hz, 1500Hz. Sample the three sinusoidal signals with a sampling frequency of 8000 Hz. nd=(20:N-1+20);
The following MATLAB program generates random signals using each distribution. i) Generate and Plot all the sinusoidal signals. y1d=sin(2*pi*(f1/fs)*nd);
PROGRAM: subplot(4,2,2);
ii) Delay the first sinusoidal signal by 20 samples.
plot(nd,y1d);
clc; iii) Advance the second sinusoidal signal by 10 samples. title("Signal-1 Delayed by 20 samples");
close all;
iv) Add the first and second sinusoidal signal. xlabel("n");
n=input("Random Signals\n Enter the number of Samples: "); ylabel("Amplitude");
v) Multiply the first and third sinusoidal signal.
y1=rand(1,n);
y2=randn(1,n); na=(-10:N-1-10);
plot(y1); y2a=sin(2*pi*(f2/fs)*na);
PROGRAM:
hold on; subplot(4,2,4);
plot(y2,"ro"); clc; plot(na,y2a);
title("Random Signals");
close all; title("Signal-2 Advanced by 10 samples");
xlabel("n");
ylabel("Amplitude"); xlabel("n");
legend('Uniform Distributed Signal','Standard Normal Distributed Signal'); disp("Signal Manipulation"); ylabel("Amplitude");
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: ");

I) COMPUTING AUTO CORRELATION OF THE GIVEN INPUT SEQUENCE: II) FIND AUTOCORRELATION OF A SINUSOIDAL SIGNAL: ALGORITHM – CROSS CORRELATION: III) CROSS-CORRELATION OF TWO DIFFERENT SINUSOIDAL SIGNALS:
PROGRAM • Get two signals x(m)and h(p)in matrix form
clc; PROGRAM: • The correlated signal is denoted as y(n) PROGRAM:
close all;
clc; • y(n)is given by the formula 𝑦𝑦(𝑛𝑛) = ∑∞ clc;
𝑘𝑘=−∞ 𝑥𝑥(𝑘𝑘)ℎ(𝑘𝑘 − 𝑛𝑛)
X=input("Enter the input sequence: "); close all; close all;
spx=input("Enter the starting point of the • Stop
disp("Autocorrelation of a Sinusoidal Signal"); fprintf("Cross Corelation of two sinusoidal
sequence:");
fi=input("Enter the frequency of the signal: "); I) COMPUTING CROSS- CORRELATION OF THE GIVEN INPUT SEQUENCE: signals\n");
len=length(X); fs=input("Enter the sampling frequency: "); II) f1=input("Enter the frequency of Signal-1: ");
subplot(2,1,1); N=input("Enter the number of samples: "); f2=input("Enter the frequency of Signal-2: ");
stem(spx:spx+len-1,X,'fill','r'); PROGRAM: fs=input("Enter the sampling frequency: ");
title("Sequence"); t=(0:0.01:7); N=input("Enter the number of samples: ");
clc;
xlabel("n"); subplot(3,1,1);
close all;
ylabel("X[n]"); y1=sin(2*pi*fi*t); n=(0:N-1);
[autocorr,lags]=xcorr(X); plot(t,y1); fprintf("Cross Corelation of two sequences\n"); subplot(3,1,1);
lags title("Input signal"); X1=input("Enter the input sequence-1: "); x=sin(2*pi*(f1/fs)*n);
autocorr xlabel("t"); spx1=input("Enter the starting point of the plot(n,x);
subplot(2,1,2); ylabel("y(t)"); sequence:"); title("Signal-1");
stem(lags,autocorr,'fill','b'); X2=input("Enter the input sequence-2: "); xlabel("n");
n=(0:N-1); ylabel("X[n]");
title("Autocorrelation of the sequence"); spx2=input("Enter the starting point of the
subplot(3,1,2);
xlabel("n"); sequence:");
y2=sin(2*pi*(fi/fs)*n); subplot(3,1,2);
ylabel("Y[n]"); 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:

Circular convolution using inbuilt command


PROGRAM: LINEAR CONVOLUTION USING FOR LOOP: for i = 1 : lx+lh-1 ALGORITHM:
for j = 1 : i
clc; ALGORITHM: y(i) = y (i)+(X(j)*H(i-j+1)); • Start the program
close all; end
• Start end • Enter the first input sequence x(n).
x=input("ENTER THE INPUT SEQUENCE 1=");
spx=input("ENTER THE STARTING POINT OF SEQUENCE 1="); • Read the first sequence subplot(1,3,3); • Enter the second input sequence h(n)
stem(spx+sph:spx+sph+ly-1, y, 'fill','g');
lx=length(x); • Read the second sequence xlabel('sample'); • Plot both the sequences in discrete form using stem function
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'); • Use the inbuilt command cconv( ) function to perform the circular convolution
xlabel('sample');
ylabel('Amplitude'); • Find the length of second sequence operation
title('INPUT SEQUENCE 1'); • Perform linear convolution for both the sequences • Finally plot the convolution sequence and the input sequence in a single window
OUTPUT: using subplot function.
h=input("ENTER THE INPUT SEQUENCE 2="); • Plot the sequence
sph=input("ENTER THE STARTING POINT OF SEQUENCE 2=");
• Display the output sequence • Stop the program
lh=length(h);
subplot(1,3,2); • Stop PROGRAM:
stem(sph:sph+lh-1,h,'fill', 'g');
xlabel('sample'); clc;
ylabel('Amplitude'); close all;
PROGRAM:
title('INPUT SEQUENCE 2');
close all; x=input("ENTER THE INPUT SEQUENCE 1=");
y=conv(x,h); clc; spx=input("ENTER THE STARTING POINT OF SEQUENCE 1=");
ly=length(y); lx=length(x);
subplot(1,3,3); x=input("ENTER THE INPUT SEQUENCE 1="); subplot(1,3,1);
stem(spx+sph:spx+sph+ly-1,y); spx=input("ENTER THE STARTING POINT OF SEQUENCE 1="); stem(spx:spx+lx-1,x,'fill','b');
xlabel('sample'); lx=length(x); xlabel('sample');
ylabel('Amplitude'); subplot(1,3,1); ylabel('Amplitude');
title('LINEAR CONVOLUTION'); stem(spx:spx+lx-1,x,'fill','b'); title('INPUT SEQUENCE 1');
xlabel('sample');
ylabel('Amplitude'); h=input("ENTER THE INPUT SEQUENCE 2=");
title('INPUT SEQUENCE 1'); sph=input("ENTER THE STARTING POINT OF SEQUENCE 2=");
OUTPUT: lh=length(h);
h=input("ENTER THE INPUT SEQUENCE 2="); subplot(1,3,2);
sph=input("ENTER THE STARTING POINT OF SEQUENCE 2="); stem(sph:sph+lh-1,h,'fill', 'r');
lh=length(h); xlabel('sample');
subplot(1,3,2); ylabel('Amplitude');
stem(sph:sph+lh-1,h,'fill', 'r'); title('INPUT SEQUENCE 2');
xlabel('sample');
ylabel('Amplitude'); y=cconv(x,h);
title('INPUT SEQUENCE 2'); ly=length(y);
subplot(1,3,3);
ly=lx+lh-1; stem(spx+sph:spx+sph+ly-1,y,'fill','g');
X= [x, zeros(1,(ly-lx))]; xlabel('sample');
H = [h, zeros(1,(ly-lh))]; ylabel('Amplitude');
y= zeros(1, lx+lh-1); title('CIRCULAR CONVOLUTION');

OUTPUT:
Circular convolution using for loop circfor=zeros(1,ly); Circular convolution using FFT ALGORITHM:
for i = 1 : ly
ALGORITHM: for j = 1 : i ALGORITHM: Input the desired frequency fd (for which sampling theorem is to be verified).
circfor(i) = circfor(i)+(X(j)*H(i-j+1));
• Start end • Start the program Generate an analog signal x(t) of frequency fd for comparison.
• Read the first sequence end • Enter the x(n) sequence. Generate over sampled, Nyquist & under sampled discrete time signals.
subplot(1,3,3);
• Read the second sequence stem(spx+sph:spx+sph+ly-1, circfor, 'fill','b'); • Enter the h(n) sequence. Plot the waveforms and hence prove sampling theorem.
xlabel('sample');
• Find the length of first sequence ylabel('Amplitude'); • Perform circular convolution using FFT function.
• Find the length of second sequence title('CIRCULAR CONVOLUTION USING fOR LOOP'); • Plot the values. PROGRAM:

• Perform circular convolution for both the sequences • Stop the program clc;
close all;
• Plot the sequence
OUTPUT: fprintf("\t\t\t\t\t\t\t\t\t\t\t\t"+"Verification of
• Display the output sequence PROGRAM:
Sampling Theorem\n");
• Stop clc; %Get the input frequency
close all; f=input("Enter the frequency of the cosine signal: ");

x=input("ENTER THE INPUT SEQUENCE 1="); %Plot the real signal


PROGRAM: subplot(2,2,1);
spx=input("ENTER THE STARTING POINT OF SEQUENCE 1=");
lx=length(x); t=(-3:0.01:3);
clc; subplot(1,3,1); signal=2*cos(2*pi*f*t);
close all; stem(spx:spx+lx-1,x,'fill','r'); plot(t,signal);
xlabel('sample'); title("Cosine signal");
x=input("ENTER THE INPUT SEQUENCE 1="); ylabel('Amplitude'); xlabel("Time(t)");
spx=input("ENTER THE STARTING POINT OF SEQUENCE 1="); title('INPUT SEQUENCE 1'); ylabel("Amplitude");
lx=length(x);
subplot(1,3,1); h=input("ENTER THE INPUT SEQUENCE 2="); %Plot the undersampled signal
stem(spx:spx+lx-1,x,'fill','r'); sph=input("ENTER THE STARTING POINT OF SEQUENCE 2="); subplot(2,2,2);
xlabel('sample'); lh=length(h); fus=f/1.5;
ylabel('Amplitude'); subplot(1,3,2); %Sampling rate = f (Less than Nquist rata
title('INPUT SEQUENCE 1'); 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);
h=input("ENTER THE INPUT SEQUENCE 2="); ylabel('Amplitude'); stem(t/fus,signal_undersampled,'b');
sph=input("ENTER THE STARTING POINT OF SEQUENCE 2="); title('INPUT SEQUENCE 2'); hold on;
lh=length(h); plot(t/fus,signal_undersampled,'r','LineWidth',0.75);
subplot(1,3,2); ly=lx+lh-1; title("Cosine signal - Undersampled");
stem(sph:sph+lh-1,h,'fill', 'g'); X= [x, zeros(1,(ly-lx))]; xlabel("n");
xlabel('sample'); H = [h, zeros(1,(ly-lh))]; ylabel("Amplitude");
ylabel('Amplitude'); circfft=ifft(fft(X).*fft(H));
title('INPUT SEQUENCE 2'); subplot(1,3,3); %Plot the Nyquist sampled signal
stem(spx+sph:spx+sph+ly-1, circfft, 'fill','b'); subplot(2,2,3);
ly=lx+lh-1; xlabel('sample'); fns=2*f;
X= [x, zeros(1,(ly-lx))]; ylabel('Amplitude'); %Sampling rate equal to Nquist rate
H = [h, zeros(1,(ly-lh))]; title('CIRCULAR CONVOLUTION USING FFT'); t=(-3*fns:0.1:3*fns);
signal_nquist=2*cos(2*pi*(f/fns)*t);
OUTPUT:

stem(t/fns,signal_nquist,'b'); LIBRARY FUNCTIONS:


hold on; %Plot the oversampled signal
plot(t/fns,signal_nquist,'r','LineWidth',0.75); SAMPLING A SINUSOIDAL SIGNAL: subplot(2,2,4); spectrum(x,N) – Magnitude of the power spectra of the signal w.r.t the sampling
title("Cosine signal - Nquist Sampled"); PROGRAM: fos=4*f;
frequency
xlabel("n"); %Sampling rate much higher than the Nquist rate
ylabel("Amplitude"); 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);
%Plot the oversampled signal stem(t/fos,signal_oversampled,'b'); fft(signal) – Provides the spectral analysis by FFT
subplot(2,2,4); fprintf("\t\t\t\t\t\t\t\t\t\t\t\t"+"Verification of hold on;
fos=4*f; Sampling Theorem\n"); plot(t/fos,signal_oversampled,'r','LineWidth',0.75);
%Sampling rate much higher than the Nquist rate %Get the input frequency title("Sine signal - Oversampled "); QUESTION:
t=(-3*fos:0.1:3*fos); f=input("Enter the frequency of the sine signal: "); xlabel("n");
signal_oversampled=2*cos(2*pi*(f/fos)*t); ylabel("Amplitude");
stem(t/fos,signal_oversampled,'b'); %Plot the real signal Generate a signal of 512 samples which consists of three co-sinusoids at frequencies
hold on; subplot(2,2,1); fprintf("\nUndersampling => Sampling frequency = f1=1000Hz, f2=1500Hz and f3=2000Hz. The sampling frequency of the signal is 10KHz.
plot(t/fos,signal_oversampled,'r','LineWidth',0.75); t=(-3:0.01:3); "+f/1.5+"\n"); Observe the spectrum using the command “spectrum”. Also sample the signal exactly
title("Cosine signal - Oversampled "); 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.
xlabel("n"); plot(t,signal); "+2*f+"\n");
ylabel("Amplitude"); title("Sine signal"); fprintf("Oversampling => Sampling frequency =
xlabel("Time(t)"); "+4*f+"\n"); PROGRAM:
fprintf("\nUndersampling => Sampling frequency = ylabel("Amplitude");
"+f/1.5+"\n"); clc;
fprintf("Nquist sampling => Sampling frequency = %Plot the undersampled signal OUTPUT: close all;
"+2*f+"\n"); subplot(2,2,2);
fprintf("Oversampling => Sampling frequency = fus=f/1.5; fprintf("\t\t\t\t\t\t\t\t\t\t\t\t"+"Sampling and Effect of
"+4*f+"\n"); %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: ");
OUTPUT: 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); Parseval’s Theorem


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 For complex valued sequences x(n) and y(n), in general
plot(f,y); clc; s)*n); If,
title("Power Density - Nyquist sampled frequency "); close all; signal=fft(signal); x(n) X(K)
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;
Then,
Aliasing\n"); f=(n1*fs)/N;
%Plot the underdamped signal %Get the input frequency signal_plot=20*log10(val_signal(1:N/2)); |x(n)| = |X(K)|
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)"); QUESTION:
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 Compute 8-point DFT of the given sequence x[n] = [1+i, 1+i, 1+i, 1+i, 0, 0, 0, 0]
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 PROGRAM:
ylabel("Power"); signal=cos(2*pi*(f1/fs)*n)+cos(2*pi*(f2/fs)*n)+cos(2*pi*(f3/f s)*n);
signal=fft(signal); close all;
s)*n); clc;
%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); fprintf("\t\t\t\t\t\t\t\t\t<strong>Computation of 8- point
n1=0:(N/2)-1; f=(n1*fs)/N;
fs=4*f3; DFT</strong>\n");
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 x=input('Enter the input sequence x[n] = '); %Get the input
signal_plot=20*log10(val_signal(1:N/2)); plot(f,signal_plot);
s)*n); sequence
plot(f,signal_plot); title("Oversampled Signal");
[y,f]=pspectrum(signal,fs); title("FFT - Sampling frequency = 10kHz"); xlabel("Frequency(Hz)");
N=length(x); % Display the length of the sequence
plot(f,y); xlabel("Frequency(Hz)"); ylabel("Power");
fprintf('\n<strong>Length of the Input Sequence:</strong> ');
title("Power Density - Oversampled Signal"); ylabel("Power"); disp(N);
xlabel("Frequency(Hz)");
OUTPUT: t=0:N-1;
ylabel("Power"); %Undersampled signal
subplot(2,2,2); subplot(3,3,1);%Plot the input sequence
OUTPUT: n=(0:N-1); fprintf("<strong>Input sequence:</strong>\n");
fs=f3/1.5; disp(x);
signal=cos(2*pi*(f1/fs)*n)+cos(2*pi*(f2/fs)*n)+cos(2*pi*(f3/f stem(t,x,'red'); %Plot the Input sequence of the given
s)*n); sequence
signal=fft(signal); title('Input Sequence');
val_signal=abs(signal); xlabel('n');
n1=0:(N/2)-1; ylabel('Value');
f=(n1*fs)/N;
signal_plot=20*log10(val_signal(1:N/2)); x_real = real(x); %Display the real value of the input
plot(f,signal_plot); sequence
title("FFT - Undersampled Signal"); fprintf("Real Value of Input Sequence: ");
xlabel("Frequency(Hz)"); disp(x_real);
ylabel("Power"); subplot(3,3,2);
stem(t,x_real,'red'); %Plot the real part of the given
%Nyquist sampled signal sequence

title('Input - Real Sequence'); fprintf('<strong>IDFT of Input Sequence</strong>\n'); VERIFICATION OF LINEARITY: VERIFICATION OF PARSEVAL’S THEOREM:
xlabel('n'); disp(idft);
ylabel('Value'); subplot(3,3,7); PROGRAM: PROGRAM:
stem(t,idft); close all; close all;
x_imag = imag(x); %Display the imaginary value of the input title('IDFT Sequence'); clc; clc;
sequence xlabel('n'); fprintf("\t\t\t\t\t\t\t\t\t<strong>VERIFICATION OF LINEARITY
fprintf('Imaginery Value of Input Sequence: '); ylabel('Value'); PROPERTY OF DFT</strong>\n"); fprintf("\t\t\t\t\t\t\t\t\t<strong>VERIFICATION OF PARSEVAL'S
disp(x_imag); THEOREM OF DFT</strong>\n");
disp("Linearity");
subplot(3,3,3); idft_real = real(idft); %Display the real value of the IDFT fprintf("\t<strong>DFT(ax1(n)+bx2(n))= disp("PARSEVAL'S THEOREM");
stem(t,x_imag); %Plot the imaginary part of the given sequence aX1(\x3C9)+bX2(\x3C9)</strong>, where\n"); fprintf("\t<strong>\x2211|x[n]|^2 =(1/N)
sequence fprintf('Real Value of IDFT: '); \x2211|X(K)|^2</strong>, where\n");
fprintf("\t\ta and b are constants\n\t\tx1(n) and x(2) are
title('Input - Imaginary Sequence'); disp(idft_real); sequences"); fprintf("\n\t\tx[n] is the sequence and X(K)is the DFT of
xlabel('n'); subplot(3,3,8); fprintf("\n\t\tX1(\x3C9)and X2(\x3C9) are DFT of x1(n) and x[n]respectively\n\n");
ylabel('Value'); stem(t,idft_real); x(2) respectively\n\n");
title('IDFT Real Sequence'); x1=input('Enter the First input sequence x1[n] = '); %Get x=input('Enter the input sequence x[n] = ');
dft=fft(x,N); %Compute DFT of the sequence using FFT xlabel('n'); N=length(x);
the two input sequences
fprintf('<strong>DFT of Input Sequence:</strong>\n'); ylabel('Value'); fprintf('Length of the Input Sequence: ');
x2=input('Enter the Second input sequence x2[n] = ');
disp(dft); disp(N);
subplot(3,3,4); idft_imag = imag(idft); %Display the imaginary value of the N1=length(x1); %Display the length of the sequences y=fft(x,N);
stem(t,dft,'red'); %Plot the DFT of the given sequence IDFT sequence fprintf('Length of the First Input Sequence: '); disp('DFT of the Input Sequence');
title('DFT'); fprintf('Imaginery Value of IDFT: '); disp(N1); disp(y);
xlabel('n'); disp(idft_imag); N2=length(x2); LHS=sum(abs(x).^2);
ylabel('Value'); subplot(3,3,9); fprintf('Length of the Second Input Sequence: '); disp('Displaying LHS');
stem(t,idft_imag,'green'); disp(N2); disp(LHS);
mag=abs(dft); %Compute the magnitude of the DFT sequence title('IDFT Imaginery Sequence'); a1=input('Enter the first coefficient a1='); RHS=sum(abs(y).^2)/N;
fprintf('Magnitude of the DFT Sequence: '); xlabel('n'); a2=input('Enter the second coefficient a2='); %Get the co- disp('Displaying RHS');
disp(mag); ylabel('Value') efficients disp(RHS);
subplot(3,3,5);
stem(t,mag,'green'); % Plot the magnitude sequence a=a1*x1 + a2*x2; if(LHS==RHS)
title('Magnitude Sequence'); OUTPUT:
LHS=fft(a); fprintf('LHS=RHS\n Hence Parsevals Theorem is
xlabel('n'); disp('Displaying LHS'); Verified\n');
ylabel('Magnitude'); disp(LHS); else
disp('Parsevals Theorem is not Proved');
phase=angle(dft); %Compute the phase of the DFT sequence X1=fft(x1,N1); end
disp('Phase angle of the DFT Sequence'); X2=fft(x2,N2);
fprintf('\tPhase Angle in Radians: '); Y1=a1*X1;
disp(phase); Y2=a2*X2;
phaseang=rad2deg(phase); %Coversion of radians to degree Y=Y1+Y2;
fprintf('\tPhase Angle in Degree: '); OUTPUT:
RHS=Y;
disp(phaseang); disp('Displaying RHS');
subplot(3,3,6); %Plot the phase angle sequence disp(RHS);
stem(t,phaseang,'red');
title('Phase Angle Sequence'); if(LHS==RHS)
xlabel('n'); fprintf('LHS=RHS\nHence,Linearity is Proved\n');
ylabel('Degrees'); else
disp('Linearity not proved');
idft=ifft(dft); %Compute the Inverse DFT end
ALGORITHM: %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
1. Get the passband and stopband ripples frequency and Order subplot(2,2,2); %Plot the response graphs 0.6.1
2. Get the passband and stopband edge frequencies disp("Butterworth IIR LPF Filter"); plot(ph/pi,20*log10(m));
fprintf("Order, N = ");disp(N); title('Chebyshev IIR LPF Filter');
3. fprintf("Cutoff Frequency, Wn = ");disp(Wn); xlabel('Normalised frequency'); transformation.
ylabel('Gain in dB');
4. Calculate the corresponding analog frequency %Design a analog Butterworth LPF of Order N and Cutoff subplot(2,2,4);
5. Calculate the order and cut-off frequency of the filter using Butterworth or frequency Wn plot(ph/pi,an); PROGRAM:
[num,den]=butter(N,Wn,'high','s'); title('Chebyshev IIR LPF Filter');
Chebyshev as per the specification [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
6. Design the analog counterpart. close all;
Transformation
7. W=0:0.01:pi; clc;
[h,ph]=freqz(B,A,W,'whole'); %Obtain the frequency OUTPUT:
8. 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");
QUESTION: 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 : ');
1. To design an IIR Butterworth and Chebyshev LPF for the following specification: 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 : ');
Ap
xlabel('Normalised frequency'); Ts=input('Enter the sampling time period in s,Ts : ');
As ylabel('Gain in dB');
subplot(2,2,3); omgp=(Wp/Ts); %Compute pre-warped analog frequency
plot(ph/pi,an); omgs=(Ws/Ts);
PROGRAM: 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
%IIR- BUTTERWORTH AND CHEBYSHEV LOWPASS FILTERS USING
%Chebyshev IIR LPF Filter disp("Butterworth IIR HPF Filter");
BILINEAR TRANSFORMATION
[N,Wn]=cheb1ord(omgp,omgs,Ap,As,'s'); %Compute Cutoff fprintf("Order, N = ");disp(N);
close all;
frequency and Order fprintf("Cutoff Frequency, Wn = ");disp(Wn);
clc;
disp("CHEBYSHEV IIR LPF Filter");
fprintf("Order, N = ");disp(N); %Design a analog Butterworth HPF of Order N and Cutoff
fprintf("\t\t\t\t\t\t\t\t\t<strong>IIR- BUTTERWORTH AND
fprintf("Cutoff Frequency, Wn = ");disp(Wn); frequency Wn
CHEBYSHEV LOWPASS FILTERS USING BILINEAR TRANSFORMATION
[num,den]=butter(N,Wn,'high','s');
</strong>\n");
%Design a analog Chebyshev LPF of Order N and Cutoff [B,A]=impinvar(num,den,(1/Ts));
Ap=input('Enter pass band ripple in dB,Ap : ');
frequency Wn %Covert analog filter into digital using Impulse
As=input('Enter stop band attenuation in dB,As : ');
[num,den]=cheby1(N,Ap,Wn,'high','s'); Invariant Transformation
Wp=input('Enter pass band edge frequency in rad, Wp : ');
%Covert analog filter into digital using Bilinear W=0:0.01:pi;
Ws=input('Enter stop band edge frequency in rad, Ws : ');
Transformation [h,ph]=freqz(B,A,W,'whole'); %Obtain the frequency
Ts=input('Enter the sampling time period in s,Ts : ');
[B,A]=bilinear(num,den,(1/Ts)); response
W=0:0.01:pi; m=abs(h);
omgp=(2/Ts)*tan(Wp/2); %Compute pre-warped analog
[h,ph]=freqz(B,A,W,'whole'); %Obtain the frequency an=angle(h);
frequency
omgs=(2/Ts)*tan(Ws/2); 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 2. To design a digital Chebyshev IIR bandstop filter using impulse invariant
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); transformation for the given specification:
ylabel('Gain in dB'); W=0:0.01:pi; Lower stop band edge frequency = 0.4 rad, Lower pass band edge frequency = 0.1 rad
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); Upper stop band edge frequency = 0.6 rad, Upper pass band edge frequency = 0.9 rad
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 Ap = 3 dB and A s = 18 dB with Fs = 500Hz.
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'); PROGRAM:
[N,Wn]=cheb1ord(omgp,omgs,Ap,As,'s'); %Compute Cutoff xlabel('Normalised frequency'); %IIR- CHEBYSHEV BANDSTOP FILTER USING IMPULSE INVARIANT
PROGRAM: ylabel('Gain in dB');
frequency and Order TRANSFORMATION
disp("CHEBYSHEV IIR HPF Filter"); %IIR- BUTTERWORTH BANDPASS FILTER USING BILINEAR subplot(2,1,2); close all;
fprintf("Order, N = ");disp(N); TRANSFORMATION plot(ph/pi,an); clc;
fprintf("Cutoff Frequency, Wn = ");disp(Wn); close all; title('Butterworth IIR BPF Filter - Phase Response');
clc; xlabel('Normalised frequency'); fprintf("\t\t\t\t\t\t\t\t<strong>IIR- CHEBYSHEV BANDSTOP
%Design a analog Chebyshev HPF of Order N and Cutoff ylabel('Angle in Radians'); FILTER USING IMPULSE INVARIANT TRANSFORMATION</strong>\n");
frequency Wn fprintf("\t\t\t\t\t\t\t\t<strong>IIR- BUTTERWORTH BANDPASS Ap=input('Enter pass band ripple in dB,Ap : '); %Get the
[num,den]=cheby1(N,Ap,Wn,'high','s'); FILTER USING BILINEAR TRANSFORMATION </strong>\n"); input
%Covert analog filter into digital using Impulse Ap=input('Enter pass band ripple in dB,Ap : '); %Get the As=input('Enter stop band attenuation in dB,As : ');
input OUTPUT:
Invariant Transformation Wpl=input('Enter lower passband edge frequency in rad : ');
[B,A]=impinvar(num,den,(1/Ts)); As=input('Enter stop band attenuation in dB,As : '); Wpu=input('Enter upper passband edge frequency in rad : ');
W=0:0.01:pi; Wpl=input('Enter lower passband edge frequency in rad : '); Wsl=input('Enter lower stopband edge frequency in rad : ');
[h,ph]=freqz(B,A,W,'whole'); %Obtain the frequency Wpu=input('Enter upper passband edge frequency in rad : '); Wsu=input('Enter upper stopband edge frequency in rad : ');
response Wsl=input('Enter lower stopband edge frequency in rad : '); Fs=input('Enter the sampling time period in Hz,Fs : ');
m=abs(h); Wsu=input('Enter upper stopband edge frequency in rad : ');
an=angle(h); Fs=input('Enter the sampling frequency in Hz,Fs : '); Wp=[Wpl Wpu]; %Vector of passband edge frequencies
Ws=[Wsl Wsu]; %Vector of stopband edge frequencies
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 omgp=(Wp*Fs); %Compute pre-warped analog frequency
title('Chebyshev IIR HPF Filter'); omgs=(Ws*Fs);
xlabel('Normalised frequency'); omgp=(2*Fs)*tan(Wp/2); %Compute pre-warped analog frequency
ylabel('Gain in dB'); omgs=(2*Fs)*tan(Ws/2); %Chebyshev IIR BSF Filter
subplot(2,2,4); [N,Wn] = cheb1ord(omgp,omgs,Ap,As,'s'); %Compute Cutoff edge
plot(ph/pi,an); %Butterworth IIR BPF Filter frequencies and Order
title('Chebyshev IIR HPF Filter'); [N,Wn]=buttord(omgp,omgs,Ap,As,'s'); %Compute Cutoff edge fprintf("\n");
xlabel('Normalised frequency'); frequencies and Order disp("Chebyshev IIR BsF Filter");
ylabel('Angle in Radians'); fprintf("\n"); fprintf("Order, N = ");disp(N);
disp("Butterworth IIR BPF Filter"); fprintf("Cutoff edge frequencies, Wn = ");disp(Wn);
OUTPUT: fprintf("Order, N = ");disp(N);
fprintf("Cutoff edge frequencies, Wn = ");disp(Wn); %Design a analog Chebyshev BSF of Order N and Cutoff
frequency Wn
%Design a analog Butterworth BPF of Order N and Cutoff edge [num,den] = cheby1(N,Ap,Wn,'stop','s');
frequencies Wn [B,A]=impinvar(num,den,Fs);
[num,den]=butter(N,Wn,'bandpass','s'); %Covert analog filter into digital using Impulse Invariant
Transformation

W=0:0.001:pi; APPENDIX: an=angle(h); 2. CHEBYSHEV BSF USING BILINEAR TRANSFORMATION METHOD


[h,ph]=freqz(B,A,W,'whole'); %Obtain the frequency response
m=abs(h); subplot(2,1,1); %Plot the response graphs
1. BUTTERWORTH BPF USING IMPULSE INVARIANT TRANFORMATION plot(ph/pi,20*log(m));
PROGRAM:
an=angle(h);
METHOD title('Butterworth IIR BPF Filter - Magnitude Response');
subplot(2,1,1); %Plot the response graphs xlabel('Normalised frequency'); %IIR- CHEBYSHEV BANDSTOP FILTER USING BILINEAR TRANSFORMATION
plot(ph/pi,20*log(m)); ylabel('Gain in dB'); close all;
title('Chebyshev IIR BSF Filter - Magnitude Response');
PROGRAM: subplot(2,1,2); clc;
xlabel('Normalised frequency'); plot(ph/pi,an);
ylabel('Gain in dB'); %IIR- BUTTERWORTH BANDPASS FILTER USING IMPULSE INVARIANT title('Butterworth IIR BPF Filter - Phase Response'); fprintf("\t\t\t\t\t\t\t\t<strong>IIR- CHEBYSHEV BANDSTOP
subplot(2,1,2); TRANSFORMATION xlabel('Normalised frequency'); FILTER USING BILINEAR TRANSFORMATION</strong>\n");
plot(ph/pi,an); close all; ylabel('Angle in Radians'); Ap=input('Enter pass band ripple in dB,Ap : '); %Get the
title('Chebyshev IIR BSF Filter - Phase Response'); clc; input
xlabel('Normalised frequency'); As=input('Enter stop band attenuation in dB,As : ');
ylabel('Angle in Radians'); fprintf("\t\t\t\t\t\t\t\t<strong>IIR- BUTTERWORTH BANDPASS Wpl=input('Enter lower passband edge frequency in rad : ');
FILTER USING IMPULSE INVARIANT TRANSFORMATION </strong>\n"); OUTPUT: Wpu=input('Enter upper passband edge frequency in rad : ');
Ap=input('Enter pass band ripple in dB,Ap : '); %Get the Wsl=input('Enter lower stopband edge frequency in rad : ');
input Wsu=input('Enter upper stopband edge frequency in rad : ');
As=input('Enter stop band attenuation in dB,As : '); Fs=input('Enter the sampling time period in Hz,Fs : ');
Wpl=input('Enter lower passband edge frequency in rad : ');
Wpu=input('Enter upper passband edge frequency in rad : '); Wp=[Wpl Wpu]; %Vector of passband edge frequencies
Wsl=input('Enter lower stopband edge frequency in rad : '); Ws=[Wsl Wsu]; %Vector of stopband edge frequencies
OUTPUT: Wsu=input('Enter upper stopband edge frequency in rad : ');
Fs=input('Enter the sampling frequency in Hz,Fs : '); omgp=(2*Fs)*tan(Wp/2); %Compute pre-warped analog frequency
omgs=(2*Fs)*tan(Ws/2);
Wp=[Wpl Wpu]; %Vector of passband edge frequencies
Ws=[Wsl Wsu]; %Vector of stopband edge frequencies %Chebyshev IIR BSF Filter
[N,Wn] = cheb1ord(omgp,omgs,Ap,As,'s'); %Compute Cutoff edge
omgp=(Wp*Fs); %Compute pre-warped analog frequency frequencies and Order
omgs=(Ws*Fs); fprintf("\n");
disp("Chebyshev IIR BsF Filter");
%Butterworth IIR BPF Filter fprintf("Order, N = ");disp(N);
[N,Wn]=buttord(omgp,omgs,Ap,As,'s'); %Compute Cutoff edge fprintf("Cutoff edge frequencies, Wn = ");disp(Wn);
frequencies and Order
fprintf("\n"); %Design a analog Chebyshev BSF of Order N and Cutoff
disp("Butterworth IIR BPF Filter"); frequency Wn
fprintf("Order, N = ");disp(N); [num,den] = cheby1(N,Ap,Wn,'stop','s');
fprintf("Cutoff edge frequencies, Wn = ");disp(Wn); [B,A]=bilinear(num,den,Fs);
%Covert analog filter into digital using Bilinear
%Design a analog Butterworth BPF of Order N and Cutoff edge Transformation
frequencies Wn W=0:0.01:pi;
[num,den]=butter(N,Wn,'bandpass','s'); [h,ph]=freqz(B,A,W,'whole'); %Obtain the frequency response
%Covert analog filter into digital using Impulse Invariant m=abs(h);
Transformation an=angle(h);
[B,A]=impinvar(num,den,Fs);
W=0:0.01:pi; subplot(2,1,1); %Plot the response graphs
[h,ph]=freqz(B,A,W,'whole'); %Obtain the frequency response plot(ph/pi,20*log(m));
m=abs(h); title('Chebyshev IIR BSF Filter - Magnitude Response');

xlabel('Normalised frequency'); LOW PASS FIR M=ceil((11*pi)/dW); HIGH PASS FIR


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

M=ceil(((6.1*pi)/dW)); QUESTION: subplot(2,2,3);


W=bartlett(M); subplot(2,2,3); output = interp(input,L);
elseif(As<=45) plot(w1/pi,20*log(abs(h)),'r','LineWidth',0.75); l=0:(L*N)-1;
disp('Hanning Window'); title("Magnitude Response of FIR-BSF"); stem(l,output,'b');
M=ceil((6.2*pi)/dW); xlabel('Normalised Frequency'); i. Generate a sinusoidal signal with 500 samples sampled at 1kHz. Increase the hold on;
W=hanning(M); ylabel('Gain in dB'); sample rate to 4kHz. plot(l,output,'r','LineWidth',0.75);
elseif(As<=53) title("Output signal");
disp('Hamming Window'); subplot(2,2,4); xlabel("n");
M=ceil((6.6*pi)/dW); plot(w1/pi,angle(h),'m','LineWidth',0.75); ylabel("Amplitude");
PROGRAM
W=hamming(M); title("Angle Response of FIR-BSF");
elseif(As<=75) xlabel('Normalized Frequency'); %MULTIRATE FILTERS %Plot the first 100 samples of Interploated Signal
disp('Blackman Window'); ylabel('Phase'); clc; subplot(2,2,4);
M=ceil((11*pi)/dW); close all; stem(l(1:L*100),output(1:L*100),'b');
W=blackman(M); hold on;
else OUTPUT: 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);
disp('Please Enter a Correct Value'); FILTERS</strong>\n"); title("Output signal - First L*100 samples");
end %Get the input frequency xlabel("n");
ylabel("Amplitude");
%Compute Order of the filter f1= input("Enter the frequency of the Sinusoidal signal:
N=M-1; ");
fprintf("Length of the filter: "); fs1=input("Enter the sampling frequency-1: ");
disp(M); N=input("Enter the number of samples: "); OUTPUT
fprintf("Order of the filter: "); fs2=input("Enter the sampling frequency-2: ");
disp(N); L= fs2/fs1;
if(mod(N,2)~= 0)
%Odd Order symmetric FIR filters must have a gain of zero n=(0:N-1); %Range N
at Nyquist %Plot the input signal
%frequency. So, the order is increased by 1. subplot(2,2,1);
N=N+1; input=3*sin(2*pi*(f1/fs1)*n);
end stem(n,input,'b');
hold on;
%Design a Bandstop FIR filter plot(n,input,'r','LineWidth',0.75);
B=fir1(N,[Wc1,Wc2],'stop'); title("Input signal");
[h,w1]=freqz(B,M); %Determine x and y axis xlabel("n");
ylabel("Amplitude");
%Plot the response graphs
subplot(2,2,1); %Plot the first 100 samples input signal
plot(B,'g','LineWidth',0.75); subplot(2,2,2);
title("Impulse Response of FIR-BSF"); stem(n(1:100),input(1:100),'b');
xlabel('Frequency'); hold on;
ylabel('Amplitude'); plot(n(1:100),input(1:100),'r','LineWidth',0.75);
title("Input signal - First 100 samples");
subplot(2,2,2); xlabel("n");
plot(W,'LineWidth',0.75); ylabel("Amplitude");
title("Window Response of FIR-BSF");
xlabel('Frequency');
ylabel('Amplitude'); %Plot the Interploated Signal

ii. Generate a sinusoidal signal with 1024 samples sampled at 4kHz. Decrease the 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);
sample rate to 1kHz. 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);
PROGRAM rate to 3kHz. title("Input signal - First 50 samples");
%Plot the first 100 samples of Decimated Signal xlabel("n");
%MULTIRATE FILTERS
subplot(2,2,4); ylabel("Amplitude");
clc;
stem(m(1:(50/M)),output(1:(50/M)),'b'); PROGRAM
close all;
hold on;
plot(m(1:(50/M)),output(1:(50/M)),'r','LineWidth',0.75); %MULTIRATE FILTERS %Plot the Interploated Signal
fprintf("\t\t\t\t\t\t\t\t\t\t\t\t<strong>MULTIRATE clc;
title("Output signal - First 50/M samples"); subplot(3,2,3);
FILTERS</strong>\n"); close all;
xlabel("n"); output = interp(input,L);
%Get the input frequency
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');
f1= input("Enter the frequency of the Sinusoidal signal: FILTERS</strong>\n"); hold on;
"); OUTPUT plot(l,output,'r','LineWidth',0.75);
fs1=input("Enter the sampling frequency-1: "); %Get the input frequency title("Interpolated Output signal");
N=input("Enter the number of samples: "); f1= input("Enter the frequency of the Sinusoidal signal- xlabel("n");
fs2=input("Enter the sampling frequency-2: "); 1: "); ylabel("Amplitude");
M= fs1/fs2; f2= input("Enter the frequency of the Sinusoidal signal-
2: "); %Plot the first 50 samples of Interploated Signal
n=(0:N-1); %Range N fs1=input("Enter the sampling frequency-1: "); subplot(3,2,4);
%Plot the input signal N=input("Enter the number of samples: "); stem(l(1:L*50),output(1:L*50),'b');
subplot(2,2,1); fi=input("Enter the Interpolation frequency: "); hold on;
input=3*sin(2*pi*(f1/fs1)*n); fd=input("Enter the Decimation frequency: "); plot(l(1:L*50),output(1:L*50),'r','LineWidth',0.75);
stem(n,input,'b');
title("Interpolated Output signal - First L*50 samples");
hold on; %Decimation and Interpolation Factors xlabel("n");
plot(n,input,'r','LineWidth',0.75); L= fi/fs1; ylabel("Amplitude");
title("Input signal"); fprintf("Interpolation Factor, L: ");
xlabel("n"); disp(L);
ylabel("Amplitude"); M= fs1/fd; %Plot the Decimated Signal
fprintf("Decimation Factor, M: "); subplot(3,2,5);
%Plot the first 50 samples input signal disp(M); output = decimate(input,M,'fir');
subplot(2,2,2); m=0:(N/M)-1;
stem(n(1:50),input(1:50),'b'); n=(0:N-1); %Range N stem(m,output,'b');
hold on; %Plot the input signal hold on;
plot(n(1:50),input(1:50),'r','LineWidth',0.75); subplot(3,2,1); plot(m,output,'r','LineWidth',0.75);
title("Input signal - First 50 samples"); input=3*sin(2*pi*(f1/fs1)*n) + 3*sin(2*pi*(f2/fs1)*n); title("Decimated Output signal");
xlabel("n"); stem(n,input,'b'); xlabel("n");
ylabel("Amplitude"); hold on; ylabel("Amplitude");
plot(n,input,'r','LineWidth',0.75);
title("Input signal"); %Plot the first 50 samples of Decimated Signal
%Plot the Decimated Signal xlabel("n"); subplot(3,2,6);
subplot(2,2,3); ylabel("Amplitude"); stem(m(1:(50/M)),output(1:(50/M)),'b');
output = decimate(input,M,'fir');
hold on;
m=0:(N/M)-1;
plot(m(1:(50/M)),output(1:(50/M)),'r','LineWidth',0.75);
stem(m,output,'b');

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


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

OUTPUT

You might also like