Bit 1
Bit 1
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:
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: ");
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
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');
OUTPUT