lab2
lab2
To generate a discrete time signal sequence (Unit step, Unit ramp, Sine,
Cosine, Exponential, Unit impulse) using MATLAB function.
APPARATUS REQUIRED:
THEORY:
If the amplitude of the signal is defined at every instant of time then it is called
continuous time signal. If the amplitude of the signal is defined at only at some
instants of time then it is called discrete time signal. If the signal repeats itself
at regular intervals then it is called periodic signal. Otherwise they are called
aperiodic signals.
Ramp signal: The ramp function is a unitary real function, easily computable as
the mean of the independent variable and its absolute value. This function is
applied in engineering. The name ramp function is derived from the
appearance of its graph.
= 0 else
Unit impulse signal: one of the more useful functions in the study of linear
systems is the “unit impulse function.” An ideal impulse function is a function
that is zero everywhere but at the origin, where it is infinitely high. However,
the area of the impulse is finite.
Y(t) = 1 when t=0
=0 otherwise
Unit step signal: the unit step function and the impulse function are
considered to be fundamental functions in engineering, and it is strongly
recommended that the reader becomes very familiar with both of these
functions.
u(t)= 0 if t<0
1 if t>0
½ if t=0
sinc(x) = 1
PROCEDURE:
%generation of sequences
clc;clear all;
close all;
N= input ('enter length of unit step sequence (N) =');
a=input('enter the scaling factor a=');
%generation unit step sequence
n=-N:1:N;
subplot (3,3,1);
y=[zeros(1,N),1,ones(1,N)];
stem (n, y);
xlabel ('time n');
ylabel ('ampliude in y');
title ('generating unit step signal');
%generation of unit ramp sequence
t=0:1: N;
subplot (3,3,2);
stem(t, t, 'r', '--');
hold on;
plot (t, t);
axis([-2 N -2 N]);
xlabel('time ');
ylabel('ampliude');
title('ramp signal');
%generation of sinusoidal sequence
t=0:0.05:N;
x=sin(2*pi*t);
subplot(3,3,3);
plot(t,x);
xlabel('time');
ylabel('amplitude');
title('analog sinusoidal signal');
subplot(3,3,4);
stem(t,x);
xlabel('time');
ylabel('amplitude');
title('discrete sinusoidal signal');
%generation of cosine sequence
t=0:0.05:N;
x=cos(2*pi*t);
subplot(3,3,5);
plot(t,x);
xlabel('time');
ylabel('amplitude');
title('analog sinusoidal signal');
subplot(3,3,6);
stem(t,x);
xlabel('time');
ylabel('amplitude');
title('discrete sinusoidal signal');
%generation of exponential sequence
t=0:1:N-1;
subplot(3,3,7);
y=exp(a*t);
stem(t,y);
xlabel('time');
ylabel('amplitude');
title('exponential sequence');
%generating of unit impulse sequence
n=-N:1:N;
subplot(3,3,8);
y=[zeros(1,N),1,zeros(1,N)];
stem(n,y);
xlabel('time n');
ylabel('ampliude in y');
title('generating impulse signal');
COMMAND WINDOW:
enter length of unit step sequence (N) =10
enter the scaling factor a=0.3
OUTPUT WAVEFORMS: (generation of discrete time signals)
0.5
ampliude in y
amplitude
ampliude
5
0.5 0
-0.5
0
0 -1
-10 -5 0 5 10 -2 0 2 4 6 8 10 0 2 4 6 8 10
time n time time
discrete sinusoidal signal analog sinusoidal signal discrete sinusoidal signal
1 1 1
amplitude
amplitude
0 0 0
-1 -1 -1
0 2 4 6 8 10 0 2 4 6 8 10 0 2 4 6 8 10
time time time
exponential sequence generating impulse signal
15 1
ampliude in y
10
amplitude
0.5
5
0 0
0 2 4 6 8 10 -10 -5 0 5 10
time time n
RESULT:Thus the MATLAB programs for discrete time signal sequence (Unit
step, Unit ramp, Sine, Cosine, Exponential, Unit impulse) using MATLAB
function was written and the results were plotted.
EXPERIMENT NO:2
EQUIPMENT REQUIRED:
Operating System-Windows10
Constructer-Simulator
THEORY:
y[n] = x[k]h[n-k]
y[n]= Convolution O.
PROCEDURE:
Command window:
Enter the input sequence [1 2 3 4 5]
Enter the impulse responce [1 2 3 4 5]
X=12345
H=12345
Linear Convolution y= 1 5 2 3 9 9 0 2 5
OUTPUT WAVEFORMS:
input sequence
5
x(n)
0
1 1.5 2 2.5 3 3.5 4 4.5 5
n
impulse response
5
h(n)
0
1 1.5 2 2.5 3 3.5 4 4.5 5
n
linear convolution
50
y(n)
0
1 2 3 4 5 6 7 8 9
n
input signal
x(t) 1
-1
0 20 40 60 80 100 120
t
impulse responce
1
h(t)
-1
0 20 40 60 80 100 120
t
linear convolution
50
y(t)
-50
0 50 100 150 200 250
t
RESULT:Thus the MATLAB program for linear convolution was performed and
the Output was verified.
EXPERIMENT NO-3
EQUIPMENT REQUIRED:
Operating System-Windows10
Constructer-Simulator
THEORY:
PROCEDURE:
clc;
clear all;
close all;
subplot(2,2,1);
stem(x);
xlabel('n value');
ylabel('amplitude');
title('X(n)');
subplot(2,2,2);
stem(h);
xlabel('n values');
ylabel('ámplitude');
title('h(n)');
N1=length(x);
N2=length(h);
N=max(N1,N2);
if(N1<N2);
x=[x,zeros(1,N-N1)];
end
if(N1>N2)
h1 [h,zeros(1,N-N2)];
end
y=zeros(1,N);
for m=0:N-1
for n=0:N-1
z=mod(m-n,N);
y(m+1)=y(m+1)+x(n+1).*h(z+1);
end
end
subplot(2,2,3);
stem(y);
xlabel('n values');
ylabel('samples');
title('circular convolution');
disp('y');
COMMAND WINDOW:
enter input sequence x(n):[1 1 2 2]
signal x(n)
3
0
0 0.5 1 1.5 2 2.5 3
signal h(n)
3
0
0 0.5 1 1.5 2 2.5 3
10
y(n)
0
0 0.5 1 1.5 2 2.5 3
n
RESULT:
Thus the MATLAB program for Circular convolution was performed and the
Output was verified.
EXPERIMENT NO-4
EQUIPMENT REQUIRED:
Operating System-Windows10
Constructer-Simulator
THEORY:
Where is the phase in radians. A MATLAB function cos (or sin) is used to
generate sinusoidal sequences. Signal addition: This is a sample-by-sample
addition given by
PROCEDURE:
PROGRAM:
clc;
clear all;
close all;
t=0:0.001:0.1;
fl=50;
x1=2*pi*fl*t;
yl=sin(x1);
figure;
subplot (3,1,1);
plot (t,yl);
title('sin(x1)');
f2=100;
x2=2*pi*f2*t;
y2=sin(x2);
subplot(3,1,2);
plot(t,y2);
title('sin(x2)');
y=y1+y2;
subplot(3,1,3);
plot(t,y);
title('sinx1=sinx2')
OUTPUT WAVEFORMS:
RESULT:
Thus the MATLAB program for sum of two sinusodial signals was performed
and the Output was verified.
EXPERIMENT NO-5
EQUIPMENT REQUIRED:
Operating System-Windows10
Constructer-Simulator
THEORY:
PROCEDURE:
PROGRAM:
clc;
clear all;
close all;
X=fft(x,N);
n=0:length(x)-1;
subplot(3,2,1);
stem(n,x);
title('input sequence');
subplot(3,2,2);
n=0:length(X)-1;
stem(n,X);
disp(X);
title('DFT');
subplot(3,2,3);
stem(n,abs(X));
title('magnitude spectrum');
Xr=ifft(x,N);
subplot(3,2,4);
stem(n,angle(X));
title('Phase spectrum');
Xr=ifft(x,N);
subplot(3,2,5);
stem(n,abs(Xr));
title('IDFT');
disp(Xr);
COMMAND WINDOW:
5 0
0 -50
0 2 4 6 8 0 2 4 6
magnitude spectrum Phase spectrum
40 5
20 0
0 -5
0 2 4 6 0 2 4 6
IDFT
4
0
0 2 4 6
RESULT:
Thus the MATLAB program for Discrete Fourier Transform(DFT) and Inverse
Discrete Fourier Transform(IDFT) was performed and the was verified.
EXPERIMENT NO-6
EQUIPMENTS:
Operating System-Windows 10
Software-MATLAB R2014a
THEORY:
PROGRAM:
clc;
clear all;
close all;
[b,a]=butter(n,wn);
[H,w]=freqz(b,a,512,fs);
mag=20*log10(abs(H));
phase=angle(H);
subplot(2,1,1);
plot(w,mag);
grid on;
ylabel('magnitude in db');
subplot(2,1,2);
plot(w,phase);
grid on;
COMMAND WINDOW:
-200
-400
0 500 1000 1500 2000 2500 3000 3500
normalized frequency in hz
phase response of the desired butterworth LPF
4
phase in radians
-2
-4
0 500 1000 1500 2000 2500 3000 3500
(b)normalised frequency in hz
RESULT:
Thus the MATLAB program to find the frequency response of the IIR low pass
Butterworth filter was performed and the Output was verified.
EXPERIMENT NO-7
EQUIPMENTS:
Operating System-Windows 10
Software-MATLAB R2014a
THEORY:
PROCEDURE:
PROGRAM:
clc;
clear all;
close all;
[b,a]=butter(n,wn,'high');
[H,w]=freqz(b,a,512,fs);
mag=20*log10(abs(H));
phase=angle(H);
subplot(2,1,1);
plot(w,mag);
grid on;
ylabel('magnitude in db');
subplot(2,1,2);
plot(w,phase);
grid on;
ylabel('phase in radians');
COMMAND WINDOW:
-200
-400
0 500 1000 1500 2000 2500 3000 3500
normalized frequency in hz
phase response of the desired butterworth HPF
4
phase in radians
-2
-4
0 500 1000 1500 2000 2500 3000 3500
(b)normalised frequency in hz
RESULT:
Thus the MATLAB program to find the frequency response of the IIR high pass
Butterworth filter was performed and the Output was verified.
EXPERIMENT-8
EQUIPMENTS:
Operating System-Windows 10
Software-MATLAB R2014a
THEORY:
PROCEDURE:
PROGRAM:
clc;
clear all;
close all;
[b,a]=cheby1(n,0.5,wn);
[H,w]=freqz(b,a,512,fs);
mag=20*log10(abs(H));
phase=angle(H);
subplot(2,1,1);
plot(w,mag);
grid on;
ylabel('magnitude in db');
subplot(2,1,2);
plot(w,phase);
grid on;
ylabel('phase in radians');
COMMAND WINDOW:
-100
-200
-300
0 500 1000 1500 2000 2500 3000 3500
normalized frequency in hz
phase response of the desired chebyshev LPF
4
phase in radians
-2
-4
0 500 1000 1500 2000 2500 3000 3500
(b)normalised frequency in hz
RESULT:Thus the MATLAB program to find the frequency response of the IIR
low pass Chebyshev filter was performed and the Output was verified.
EXPERIMENT-9
EQUIPMENTS:
Operating System-Windows 10
Software-MATLAB R2014a
THEORY:
PROCEDURE:
PROGRAM:
clc;clear all;
close all;
disp('enter the IIR filter design specifications');
[b,a]=cheby1(n,0.5,wn,'high');
[H,w]=freqz(b,a,512,fs);
mag=20*log10(abs(H));
phase=angle(H);
subplot(2,1,1);
plot(w,mag);
grid on;
ylabel('magnitude in db');
subplot(2,1,2);
plot(w,phase);
grid on;
xlabel('(b)normalised frequency in hz');
ylabel('phase in radians');
COMMAND WINDOW:
-100
-200
-300
-400
0 500 1000 1500 2000 2500 3000 3500
normalized frequency in hz
phase response of the desired chebyshev HPF
4
phase in radians
-2
-4
0 500 1000 1500 2000 2500 3000 3500
(b)normalised frequency in hz
RESULT: Thus the MATLAB program to find the frequency response of the
IIRhigh pass Chebyshev filter was performed and the Output was verified.
EXPERIMENT-10
EQUIPMENTS:
Operating System-Windows 10
Software-MATLAB R2014a
THEORY:
FIR filters are digital filters with finite impulse response. They are also
known as non recursive digital filters as they do not have the feedback. An FIR
filter has two important advantages over an IIR design: □ Firstly, there is no
feedback loop in the structure of an FIR filter. Due to not having a feedback
loop, an FIR filter is inherently stable. Meanwhile, for an IIR filter, we need to
check the stability. Secondly, an FIR filter can provide a linear-phase response.
Matched filters perform a cross correlation between the input signal and
a known pulse-shape. The FIR convolution is a cross-correlation between the
input signal and a time-reversed copy of the impulse-response. Therefore, the
matched-filter's impulse response is "designed" by sampling the known pulse
shape and using those samples in reverse order as the coefficients of the filter.
PROCEDURE:
PROGRAM:
clc;clear all;
close all;
num=-20*log10(sqrt(rp*rs));
dem=14.6*(fs-fp)/f;
n=ceil(num/dem);
n1=n+1;
if(rem(n,2)~=0);
n1=n;
n=n-1;end
y=boxcar(n1);
b=fir1(n,wp,y);
[h,om]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,1,1);
plot(om/pi,m);
xlabel('frequency');
ylabel('gain');
title('rectangular window for low pass filter magnitude response');
an=angle(h);
subplot(2,1,2);
plot(om/pi,an);
xlabel('frequency');
ylabel('phase');
COMMAND WINDOW:
OUTPUT WAVEFORMS:
rectangular window for low pass filter magnitude response
50
0
gain
-50
-100
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
frequency
rectangular window for low pass filter phase response
4
2
phase
-2
-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
frequency
RESULT:Thus the MATLAB program to find the frequency response of the FIR
low pass filter using rectangular window was performed and the Output was
verified.
EXPERIMENT-11
EQUIPMENTS:
Operating System-Windows 10
Software-MATLAB R2014a
THEORY:
PROCEDURE:
PROGRAM:
clc;
clear all;
close all;
wp=2*fp/f;
ws=2*fs/f;
num=-20*log10(sqrt(rp*rs));
dem=14.6*(fs-fp)/f;
n=ceil(num/dem);
n1=n+1;
if(rem(n,2)~=0);
n1=n;
n=n-1;
end
y=triang(n1);
b=fir1(n,wp,y);
[h,om]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,1,1);
plot(om/pi,m);
xlabel('frequency');
ylabel('gain');
an=angle(h);
subplot(2,1,2);
plot(om/pi,an);
xlabel('frequency');
ylabel('phase');
COMMAND WINDOW:
OUTPUT WAVEFORMS:
0
gain
-20
-40
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
frequency
Triangular window for low pass filter phase response
4
2
phase
-2
-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
frequency
RESULT:Thus the MATLAB program to find the frequency response of the IIR
low pass filter using triangular window was performed and the Output was
verified.
EXPERIMENT-12
EQUIPMENTS:
Operating System-Windows 10
Software-MATLAB R2014a
THEORY:
The Fast Fourier Transform is useful to map the time-domain sequence into a
continuous function of a frequency variable. The FFT of a sequence {x(n)} of
length N is given by a complex-valued sequence X(k).
PROGRAM:
clc;clear all;
close all;
tic;
X=fft(x,n)
xlabel('n --->');
ylabel('x(n) -->');grid;
subplot(1,2,2);stem(X);
grid;
COMMAND WINDOW:
X = Columns 1 through 6
Columns 7 through 8
OUTPUT WAVEFORMS:
0.45
1
0.4
0.35
0.5
Imaginary axis -->
0.3
x(n) -->
0.25 0
0.2
-0.5
0.15
0.1
-1
0.05
0 -1.5
0 2 4 6 8 0 2 4 6 8
n ---> Real axis --->
EQUIPMENTS:
Operating System-Windows 10
Software-MATLAB R2014a
THEORY:
PROCEDURE:
PROGRAM:
t = 0:0.001:0.6;
x = sin(2*pi*50*t)+sin(2*pi*120*t);
y = x + 2*randn(size(t));
subplot(2,1,1);
% figure(1);
stem(1000*t(1:50),y(1:50))
xlabel('time (milliseconds)');
Y = fft(y,512);
f = 1000*(0:256)/512;
subplot(2,1,2);
% figure(2);
stem(f,Pyy(1:257))
xlabel('frequency (Hz)');
OUTPUT WAVEFORMS: