Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DSP Lab Report

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

DSP LAB REPORT

Submitted by
Sudeesh v s
Signal
Processing(evening)

EXPERIMENTS

SL.NO TITLE
1 Generation of signals
2 Linear Convolution, Circular Convolution and Linear Convolution using Circular
Convolution.
3 Find the DFT and IDFT for the given input sequence
4 Find FFT and IFFT for the given input sequence
5 Linear convolution using DFT(Overlap-add and Overlap-Save methods).
6 FIR Filter (Low-pass, High-pass and Band-pass) design (Window Method).
7 IIR Filter (Low-pass, High-pass and Band-pass) design (Butterworth and Chebychev).
8 Generation of AM, FM & PWM waveforms and their spectrum
9 Generation of DTMF signal.
10 Study of sampling rate conversion (Decimation, Interpolation, Rational factor).
11 Filtering of noisy signals
Experiment no:-1

Title:-Generation of signals
Aim:-Write a program in MATLAB to generate the following waveforms (Discrete –
Time signal and Continuous – Time signal)

Discrete
1. Unit Impulse sequence
2. Unit step sequence
3. Unit Ramp sequence
4. Sinusoidal sequence
5. Exponential sequence
6. Random sequence
Continuous
1. Pulse signal,
2. Unit step signal
3. Ramp signal
4. Sinusoidal signal,
5. Exponential signal
6.Random signal
Program:-

%WAVE FORM GENERATION


%CT SIGNAL
%UNIT IMPULSE
clc;
clear all;
close all;
close all;
t1=-3:1:3;
x1=[0,0,0,1,0,0,0];
subplot(2,3,1);
plot(t1,x1);
xlabel('time');
ylabel('Amplitude');
title('Unit impulse signal');
%UNIT STEP SIGNAL
t2=-5:1:25;
x2=[zeros(1,5),ones(1,26)];
subplot(2,3,2);
plot(t2,x2);
xlabel('time');
ylabel('Amplitude');
title('Unit step signal');
%EXPONENTIAL SIGNAL
a=.04
t3=-10:1:20;
x3=exp(-1*a*t3);
subplot(2,3,3);
plot(t3,x3);
xlabel('time');
ylabel('Amplitude');
title('Exponential signal');
%UNIT RAMP SIGNAL
t4=-10:1:20;
x4=t4;
subplot(2,3,4);
plot(t4,x4);
xlabel('time');
ylabel('Amplitude');
title('Unit ramp signal');
%SINUSOIDAL SIGNAL
A=10
f=100
t5= 0 : 1/1000 : 0.1;
t1 = 1/f;
x5=A*sin(2*pi*t5/t1);
subplot(2,3,5);
plot(t5,x5)
xlabel('time');
ylabel('Amplitude');
title('Sinusoidal signal');
%RANDOM SIGNAL
t6=-10:1:20;
x6=rand(1,31);
subplot(2,3,6);
plot(t6,x6);
xlabel('time');
ylabel('Amplitude');
title('Random signal');

output:-

%WAVE FORM GENERATION


%DT SIGNAL
%UNIT IMPULSE
clc;
clear all;
close all;
n1=-3:1:3;
x1=[0,0,0,1,0,0,0];
subplot(2,3,1);
stem(n1,x1);
xlabel('time');
ylabel('Amplitude');
title('Unit impulse signal');
%UNIT STEP SIGNAL
n2=-5:1:25;
x2=[zeros(1,5),ones(1,26)];
subplot(2,3,2);
stem(n2,x2);
xlabel('time');
ylabel('Amplitude');

title('Unit step signal');


%EXPONENTIAL SIGNAL
a=5
n3=-10:1:20;
x3=power(a,n3);
subplot(2,3,3);
stem(n3,x3);
xlabel('time');
ylabel('Amplitude');
title('Exponential signal');
%UNIT RAMP SIGNA
n4=-10:1:20;
x4=n4;
subplot(2,3,4);
stem(n4,x4);
xlabel('time');
ylabel('Amplitude');
title('Unit ramp signal');
%SINUSOIDAL SIGNAL
A=10
f=100
t5= 0 : 2/1000 : .1;
t1 = 1/f;
x5=A*sin(2*pi*t5/t1);
subplot(2,3,5);
stem(t5,x5);
xlabel('time');
ylabel('Amplitude');
title('Sinusoidal signal');
%RANDOM SIGNAL
n6=-10:1:20;
x6=rand(1,31);
subplot(2,3,6);
stem(n6,x6);
xlabel('time');
ylabel('Amplitude');
title('Random signal');

output:-
Experiment no:-2

Title:-Linear Convolution, Circular Convolution and Linear Convolution using Circular


Convolution.
Aim:-Write a MATLAB Script to perform discrete convolution (Linear Convolution,
Circular Convolution and Linear Convolution using Circular Convolution.) for the given
two sequences
program:-

% Linear Convolution
%using build infunction
clc;
clear all;
close all;
%Program to perform Linear Convolution
x1=input('Enter the first sequence to be convoluted:');
subplot(3,1,1);
stem(x1);
xlabel('Time');
ylabel('Amplitude');
itle('First sequence');
x2=input('Enter the second sequence to be convoluted:');
subplot(3,1,2);
stem(x2);
xlabel('Time');
ylabel('Amplitude');
title('Second sequence');
f=conv(x1,x2);
disp('The Linear convoluted sequence is');
disp(f);
subplot(3,1,3);
stem(f);
xlabel('Time');
ylabel('Amplitude');
title('Linear Convoluted sequence');
output:-
Enter the first sequence to be convoluted:[1,2,3,4,5,6]
Enter the second sequence to be convoluted:[2,1,4,5,4]
The Linear convoluted sequence is
2 5 12 24 40 56 58 65 50 24
% Linear Convolution
%with out build in function
clc;
clear;
x=input('Enter the sequence x(n) ');
L=length(x);
h=input('Enter the sequence h(n) ');
M=length(h);
N=max(L,M);
X=[x,zeros(1,N-L)];
H=[h,zeros(1,N-M)];
Y=[zeros(1,N)];

for n=1:N
for m=1:N
if(n-m<0)
k=N+n-m+1;
else
k=n-m+1;
end
Y(n)=Y(n)+(X(k)*H(m));
end
end
disp(Y);
t=0:N-1;
subplot(2,2,1);stem(t,X);title('x(n)');xlabel('n');ylabel('amplitude');
subplot(2,2,2);stem(t,H);title('h(n)');xlabel('n');ylabel('amplitude');
subplot(2,2,3);stem(t,Y);title('y(n)');xlabel('n');ylabel('amplitude');
output:-
Enter the sequence x(n) [1,2,3,4,6,0,1]
Enter the sequence h(n) [1,0,6,3,2,1]
30 24 18 21 33 38 57
% Circular Convolution
clc;
clear;
x=input('Enter the sequence x(n) ');
L=length(x);
h=input('Enter the sequence h(n) ');
M=length(h);
N=max(L,M);
X=[x,zeros(1,N-L)];
H=[h,zeros(1,N-M)];
Y=[zeros(1,N)];

for n=1:N
for m=1:N
if(n-m<0)
k=N+n-m+1;
else
k=n-m+1;
end
Y(n)=Y(n)+(X(k)*H(m));
end
end
disp(Y);
t=0:N-1;
subplot(2,2,1);stem(t,X);title('x(n)');xlabel('n');ylabel('amplitude');
subplot(2,2,2);stem(t,H);title('h(n)');xlabel('n');ylabel('amplitude');
subplot(2,2,3);stem(t,Y);title('y(n)');xlabel('n');ylabel('amplitude');
output:-
Enter the sequence x(n) [1,2,3,4,6]
Enter the sequence h(n) [1,2,3,4,1]
39 41 38 26 32
%Linear Convolution using Circular Convolution
x = [2 1 2 1];
y = [1 2 3];
clin = conv(x,y);
xpad = [x zeros(1,6-length(x))];
ypad = [y zeros(1,6-length(y))];
ccirc = ifft(fft(xpad).*fft(ypad));
subplot(2,1,1)
stem(clin,'filled')
ylim([0 11])

title('Linear Convolution of x and y')


subplot(2,1,2)
stem(ccirc,'filled')
ylim([0 11])
title('Circular Convolution of xpad and ypad')

N = length(x)+length(y)-1;
xpad = [x zeros(1,12-length(x))];
ypad = [y zeros(1,12-length(y))];
ccirc = ifft(fft(xpad).*fft(ypad));
ccirc = ccirc(1:N);
ccirc2 = cconv(x,y,6);
Experiment no:-3

Title:-Find the DFT and IDFT for the given input sequence
Aim:-Write a MATLAB program to perform the Discrete Fourier Transform for the
given sequences.
Program:-
%dft
clc;
clear;
N=input('Enter the number of samples : ');
x=input('Enter the sequence to find DFT :');
L=length(x);
while(N<L)
x=input('Invalid Sequence given... Enter another sequence to find DFT :');
L=length(x);
end
if(L<N)
xn=[x,zeros(1,N-L)];
else
xn=x;
end
XK=[zeros(1,N)];
for k=0:N-1
for n=0:N-1;
XK(k+1)=XK(k+1)+(xn(n+1)*exp(-2*i*pi*n*k/N));
end
end
XK
t=0:N-1;
subplot(3,2,1);stem(t,xn);title('x(n)');xlabel('n');ylabel('amplitude');
subplot(3,2,2);stem(t,XK);title('X(K)');xlabel('n');ylabel('amplitude');
subplot(3,2,3);stem(t,real(XK));title('Real part of X(K)');xlabel('n');ylabel('amplitude');
subplot(3,2,4);stem(t,imag(XK));title('Imaginary Part of X(K)');xlabel('n');ylabel('amplitude');
subplot(3,2,5);stem(t,abs(XK));title('Magnitude Plot of X(K)');xlabel('n');ylabel('amplitude');
subplot(3,2,6);stem(t,angle(XK));title('Phase plot of X(K)');xlabel('n');ylabel('amplitude');

output:-
Enter the number of samples : 5
Enter the sequence to find DFT :[1,3,4,5,6]
XK =

19.0000 + 0.0000i -3.5000 + 3.4410i -3.5000 + 0.8123i -3.5000 - 0.8123i -3.5000 - 3.4410i
%IDFT
clc;
clear;
N=input('Enter the number of samples : ');
X=input('Enter the sequence to find IDFT :');
L=length(X);
while(N<L)
X=input('Invalid Sequence given... Enter another sequence to find DFT :');
L=length(X);
end
if(L<N)
XK=[X,zeros(1,N-L)];
else
XK=X;
end

xn=[zeros(1,N)];

for n=0:N-1
for k=0:N-1;
xn(n+1)=xn(n+1)+(XK(k+1)*exp(2*i*pi*n*k/N));
end
xn(n+1)=xn(n+1)/N;
end
xn

t=0:N-1;
subplot(3,2,1);stem(t,XK);title('X(k)');xlabel('n');ylabel('amplitude');
subplot(3,2,2);stem(t,xn);title('x(n)');xlabel('n');ylabel('amplitude');
subplot(3,2,3);stem(t,real(xn));title('Real part of x(n)');xlabel('n');ylabel('amplitude');
subplot(3,2,4);stem(t,imag(xn));title('Imaginary Part of x(n)');xlabel('n');ylabel('amplitude');
subplot(3,2,5);stem(t,abs(xn));title('Magnitude Plot of x(n)');xlabel('n');ylabel('amplitude');
subplot(3,2,6);stem(t,angle(xn));title('Phase plot of x(n)');xlabel('n');ylabel('amplitude');

OUTPUT:
Enter the number of samples : 5
Enter the sequence to find IDFT :[19.0000 + 0.0000i ,-3.5000 + 3.4410i, -3.5000 + 0.8123i, -3.5000 - 0.8123i , -3.5000 - 3.4410i]
xn =

1.0000 - 0.0000i 3.0000 + 0.0000i 4.0000 + 0.0000i 5.0000 - 0.0000i 6.0000 - 0.0000i
Experiment no:-4

Title:-Find FFT and IFFT for the given input sequence.

Aim:-Write a MATLAB Script to compute Discrete Fourier Transform and Inverse


Discrete Fourier Transform of the given sequence using FFT algorithms (FFT &
IFFT).

Program:-
%FFT AND IFFT WITH BUILD IN FUNCTION
clc
clear all
close all
mm=[];
A=10;
fs=1000;
Ts=1/fs;
t=(1:100)*Ts;
y=(A*sin(2*pi*100*t))+(A*sin(2*pi*10*t))+(A*sin(2*pi*70*t));
N=length(t);
N=length(t);
yy=fft(y);
yyy=fftshift(yy);
f=fs.*(-N/2:N/2-1)/N;
figure(1)
plot(f,(yyy.*conj(yyy)/(N*N)));
title('FFT')
y1=ifft(yy);
figure(2)
plot(t,y1),grid on;
title('IFFT')

OUTPUT:

%FFT AND IFFT WITH OUT FUNCTION


clc;
close all;
clear all;
x = input('Enter the sequence for FFT = ');
N = length(x); %finding the length of entered sequence for N point FFT
for k = 1:N %for every value of k
y(k) = 0;
for n = 1:N %for every value of n
y(k) = y(k)+x(n)*exp(-1i*2*pi*(k-1)*(n-1)/N);
%as per FFT formula 0 to n-1
end
end
t = 0:N-1;
subplot(3,1,1);
stem(t,x); % for discrete plotting
%plotting input sequence
ylabel('Amplitude');
xlabel('n');
title('Input');
magnitude = abs(y); % absolute values of individual FFT complex
disp('FFT : ');
disp(magnitude);

t = 0:N-1;
subplot(3,1,2);
stem(t,magnitude);
%plotting FFT sequence
ylabel('Amplitude');
xlabel('K');
title('FFT');
R = length(y); %length of the fft array

for n = 1:R
x1(n) = 0;
for k = 1:R %loop as per the IFFT formula
x1(n) = x1(n)+(1/R)*y(k)*exp(1i*2*pi*(k-1)*(n-1)/R);
end
end
t = 0:R-1;
subplot(3,1,3);
stem(t,x1);
%ploting IFFT sequence
disp('IFFT :');
disp(x1);
ylabel('Amplitude');
xlabel('n');
title('IFFT');

OUTPUT:-
Enter the sequence for FFT = [1 2 3 4 5]
FFT :
15.0000 4.2533 2.6287 2.6287 4.2533
IFFT :
1.0000 - 0.0000i 2.0000 - 0.0000i 3.0000 - 0.0000i 4.0000 + 0.0000i 5.0000 + 0.0000i
Experiment no:-5

Title:-Linear convolution using DFT(Overlap-add and Overlap-Save methods).

Aim:-Write a MATLAB Script to compute Linear convolution using DFT(Overlap-add


and Overlap-Save methods) of the given sequences.
Program:-

%overlap add
close All
clear All
clc
x=rand(1,30); % Random 30 Numbers
h=input('h=');
L=input('L=');
N1=length(x);
M=length(h);
lc=conv(x,h);
x=[x zeros(1,mod(-N1,L))];
N2=length(x);
h=[h zeros(1,L-1)];
H=fft(h,L+M-1);
S=N2/L;
index=1:L;
X=[zeros(M-1)];
for stage=1:S
xm=[x(index) zeros(1,M-1)]; % Selecting sequence to process
X1=fft(xm,L+M-1);
Y=X1.*H;
Y=ifft(Y);
Z=X((length(X)-M+2):length(X))+Y(1:M-1); %Samples Added in every stage
X=[X(1:(stage-1)*L) Z Y(M:M+L-1)];
index=stage*L+1:(stage+1)*L;
end
i=1:N1+M-1;
X=X(i);
similarity=corrcoef(X,lc) % Similarity b/w Inbuilt and Calculated one
figure()
subplot(2,1,1)
stem(lc);
title('Convolution Using conv() function')
xlabel('n');
ylabel('y(n)');
subplot(2,1,2)
stem(X);
title('Convolution Using Overlap Add Method')
xlabel('n');
ylabel('y(n)');

output:-
h=5
L=7
similarity =

1.00000 1.00000
1.00000 1.00000
%Overlap -save
close All
clear All
clc
x=rand(1,30);
h=input('h=');
L=input('L=');
N1=length(x);
M=length(h);
lc=conv(x,h);
x=[x zeros(1,mod(-N1,L)) zeros(1,L)];
N2=length(x);
h=[h zeros(1,L-1)];
H=fft(h,L+M-1);
S=N2/L;
index=1:L;
xm=x(index); % For first stage Special Case
x1=[zeros(1,M-1) xm]; %zeros appeded at Start point
X=[];
for stage=1:S
X1=fft(x1,L+M-1);
Y=X1.*H;
Y=ifft(Y);
index2=M:M+L-1;
Y=Y(index2); %Discarding Samples
X=[X Y];
index3=(((stage)*L)-M+2):((stage+1)*L); % Selecting Sequence to process
if(index3(L+M-1)<=N2)
x1=x(index3);
end
end;
i=1:N1+M-1;
X=X(i);
similarity=corrcoef(X,lc) % Similarity between Inbuilt and Calculated conv
figure()
subplot(2,1,1)
stem(lc);
title('Convolution Using conv() function')
xlabel('n');
ylabel('y(n)');
subplot(2,1,2)
stem(X);
title('Convolution Using Overlap Save Method')
xlabel('n');
ylabel('y(n)');
output:-
h=5
L=4
similarity =

1.00000 1.00000
1.00000 1.00000
Experiment no:-10

Title:-Study of sampling rate conversion (Decimation,Interpolation, Rational factor).

Aim:-Write a MATLAB Script to perform sampling rate conversion for any given
arbitrary sequence (D.T) or signal (C.T) by interpolation, decimation, upsampling,
downsampling and resampling , Rational factor

Program:-
clc;
clear all;
close all;
%continuous sinusoidal signal
a=input('Enter the amplitude:');
f=input('Enter the Timeperiod:');
t=-10:1:20;
x=a*sin(2*pi*f*t);
subplot(2,3,1);
plot(t,x);
xlabel('time');
ylabel('Amplitude');
title('Sinusoidal signal');
%decimating the signal
d=input('Enter the value by which the signal is to be decimated:');
y1=decimate(x,d);
subplot(2,3,2);
stem(y1);
xlabel('time');
ylabel('Amplitude');
title('Decimated signal');
%interpolating the signal
i=input('Enter the value by which the signal is to be interpolated:');
y2=interp(x,i);
subplot(2,3,3);
stem(y2);
xlabel('time');
ylabel('Amplitude');
title('Interpolated signal');
%resampling the signal
y3=resample(x,3,2);
subplot(2,3,4);
stem(y3);
xlabel('time');
ylabel('Amplitude');
title('Resampled signal');
%downsampling the signal
y4=downsample(x,2);
subplot(2,3,5);
stem(y4);
xlabel('time');
ylabel('Amplitude');
title('Downsampled signal');
%upsampling the signal
y5=upsample(x,3);
subplot(2,3,6);
stem(y5);
xlabel('time');
ylabel('Amplitude');
title('Upsampled signal');
output:-
Enter the amplitude:
5
Enter the Timeperiod:
20
Enter the value by which the signal is to be decimated:
4
Enter the value by which the signal is to be interpolated:
4
Experiment no:-8

Title:-Generation of AM, FM &amp; PWM waveforms and their


spectrum.

Aim:-Write a MATLAB Script to Generate AM, FM and PWM waveforms and their
spectrum.

AM modulation

clc;
close all;
clear all;
%XXXXXXXXXXXXXXXXXXXXXXXXXXX Define AM modulation Index
XXXXXXXXXXXXXXXXXXX
disp(' example: m=1 means 100% modulation');
%m=input(' Enter the value of modulation index (m) = ');
m=1; % for 100% modulation
if (0>m||m>1)
error('m may be less than or equal to one and geter than to zero');
end
%XXXXXXXXXXXXXXXXX modulating signal generation XXXXXXXXXXXXXXXXXXXXXXXXXX
Am=5; % Amplitude of modulating signal
fa=2000; % Frequency of modulating signal
Ta=1/fa; % Time period of modulating signal
t=0:Ta/999:6*Ta; % Total time for simulation
ym=Am*sin(2*pi*fa*t); % Eqation of modulating signal
figure(1)
subplot(3,1,1);
plot(t,ym), grid on;% Graphical representation of Modulating signal
title ( ' Modulating Signal ');
xlabel ( ' time(sec) ');
ylabel (' Amplitud(volt) ');
%XXXXXXXXXXXXXXXXXXXXX carrier signal generation XXXXXXXXXXXXXXXXXXXXXXXXXX
Ac=Am/m;% Amplitude of carrier signal [ where, modulation Index (m)=Am/Ac ]
fc=fa*10;% Frequency of carrier signal
Tc=1/fc;% Time period of carrier signal
yc=Ac*sin(2*pi*fc*t);% Eqation of carrier signal
subplot(3,1,2);
plot(t,yc), grid on;% Graphical representation of carrier signal
title ( ' Carrier Signal ');
xlabel ( ' time(sec) ');
ylabel (' Amplitud(volt) ');
%XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX AM Modulation
XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
y=Ac*(1+m*sin(2*pi*fa*t)).*sin(2*pi*fc*t); % Equation of Amplitude
%modulated signal
subplot(3,1,3);
plot(t,y);% Graphical representation of AM signal
title ( ' Amplitude Modulated signal ');
xlabel ( ' time(sec) ');
ylabel (' Amplitud(volt) ');
grid on;
figure(2)
x=fft(y)
plot(t,abs(x));
title ( 'spcetrum Amplitude Modulated signal ');
xlabel ( ' time(sec) ');
ylabel (' freq ');

output:

FM modulation
clc
clear all
close all
t = 0:255;
t = t/256;
f = 2;
fc = 20;
beta = 5;
m = sin(2*pi*f*t);
xc = cos(2*pi*fc*t + beta*m);
figure(6)
plot (t,xc)
figure(4)
plots.t = 0:255;
t = t/256;
f = 2;
fc = 20;
beta = 2;
m = sin(2*pi*f*t);
xc = cos(2*pi*fc*t + beta*m);

fo = zeros(1,256);
ff = zeros(1,256);
ff = fc + 4*cos(2*pi*f*t);
x = zeros(1,256);
disc = zeros(1,256);
fo(1) = 20;

for i=1:256
disc(i) = 2*pi*fc*t(i) + beta*m(i) - 2*pi*fo(i)*t(i);
if (i ~= 1)
x(i) = (disc(i) - disc(i-1))*256/(2*pi);
else
x(i) = disc(i)*256/(2*pi);
end;
if (i ~= 256)
fo(i+1) = fc + 0.001*x(i);
end;
end;

subplot(2,1,1)
plot(t,disc);
ylabel('Discriminator');
title('Output of Discrminator');
subplot(2,1,2)
plot(t,x);
ylabel('x');
title('Adjustment to VCO');

figure(9)
x=fft(xc)
plot(abs(x))
title('spectrum of fm');

output:-
PWM modulation
clc;
clear all;
t = 0:0.001:1;
fc = input('Enter the Frequency of Carrier Signal (Sawtooth) = ');
fm = input('Enter the Frequency of Message Signal (Sinusoidal) = ');
a = input('Enter the Amplitude of Carrier Signal = ');
b = input('Enter the Amplitude of Message Signal(should be < Carrier) = ');
vc = a.*sawtooth(2*pi*fc*t);
vm = b.*sin(2*pi*fm*t);
n = length(vc);
for i = 1:n
if (vm(i)>=vc(i))
pwm(i) = 1;
else
pwm(i) = 0;
end
end
% Representation of the Message Signal
subplot(3,1,1);
plot(t,vm,'black');
xlabel('Time ----->');
ylabel('Amplitude ----->');
title('Message Signal');
legend('Message Signal ---->');
grid on;
% Representation of the Carrier Signal
subplot(3,1,2);
plot(t,vc);
xlabel('Sample ----->');
ylabel('Amplitude ----->');
title('Carrier Signal');
legend('Carrier Signal ---->');
grid on;
% Representation of the PWM Signal
subplot(3,1,3);
plot(t,pwm,'red');
xlabel('Sample ----->');
ylabel('Amplitude ----->');
title('PWM Signal');
legend('PWM Signal ---->');
axis([0 1 0 2]);
grid on;
% Add title to the Overall Plot
ha = axes ('Position',[0 0 1 1],'Xlim',[0 1],'Ylim',[0 1],'Box','off','Visible','off','Units','normalized', 'clipping' , 'off');
text (0.5, 1,'\bf Pulse Width Modulation ','HorizontalAlignment','center','VerticalAlignment', 'top')
output:-

You might also like