Lab 1
Lab 1
clc;
clear all;
close all;
n=0:1:10;
s=ones(1,11);
subplot(2,1,1);
stem(n,s);
xlabel('n');
ylabel('amplitude');
title('discrete unit step sequence');
subplot(2,1,2);
plot(n,s);
xlabel('n');
ylabel('amplitude');
title('continuous unit step signal');
amplitude
0.5
5
6
7
n
continuous unit step signal
10
10
amplitude
1.5
1
0.5
0
5
n
Unit Impulse
clc;
clear all;
n=-5:1:5;
i=[zeros(1,5),ones(1,1),zeros(1,5)];
stem(n,i);
xlabel('n');
ylabel('amplitude');
title('unit impulse sequence');
amplitude
0.6
0.5
0.4
0.3
0.2
0.1
0
-5
-4
-3
-2
-1
0
n
Sinusoidal signals:
clc;
clear all;
n=0:0.001:10;
f=0.5;
s=sin(2*pi*f*n);
c=cos(2*pi*f*n);
subplot(2,1,1);
plot(n,s);
xlabel('time');
ylabel('amplitude');
title('sine signal');
subplot(2,1,2);
plot(n,c);
xlabel('time');
ylabel('amplitude');
title('cosine signal');
sine signal
1
amplitude
0.5
0
-0.5
-1
5
6
time
cosine signal
10
10
amplitude
0.5
0
-0.5
-1
5
time
Square signal
clc;
clear all;
close all;
n=0:0.01:10;
f=0.5;
s=square(2*pi*f*n);
plot(n,s);
axis([0 10 -2 2]);
xlabel('time');
ylabel('Amplitude');
title('square signal');
square signal
2
1.5
1
Amplitude
0.5
0
-0.5
-1
-1.5
-2
6
time
10
12
Rectangular signal:
clc;
clear all;
close all;
n=0:0.01:10;
f=0.5;
s=square(2*pi*f*n,70);
plot(n,s);
axis([0 10 -2 2]);
xlabel('time');
ylabel('Amplitude');
title('rectangular signal');
rectangular signal
2
1.5
1
Amplitude
0.5
0
-0.5
-1
-1.5
-2
5
time
10
Ramp
clc;
clear all;
n=0:1:10;
l=length(n)-1;
pr=n;
nr=l-n;
subplot(2,2,1);
stem(n,pr);
xlabel('time');ylabel('Amplitude');
title('positive ramp sequence');
subplot(2,2,2);
plot(n,pr);
xlabel('time');
ylabel('Amplitude');
title('positive ramp signal');
subplot(2,2,3);
plot(n,nr);
xlabel('time');
ylabel('Amplitude');
title('negative ramp signal');
subplot(2,2,4);
stem(n,nr);
xlabel('time');
ylabel('Amplitude');
title('negative ramp sequence');
positive ramp signal
Amplitude
Amplitude
10
5
time
negative ramp
10
5
time
negative ramp
10
5
time
10
10
Amplitude
Amplitude
10
Sawtooth wave
5
time
10
clc;
clear all;
close all;
fs = 10000;
t = 0 : 1/fs : 1.5;
x = sawtooth (2 * pi * 50 * t , 0);
y = sawtooth (2*pi* 50 * t);
subplot(2,1,1);
plot(t,x); axis ( [ 0 0.1 -1 1 ] );
xlabel ('Time');
ylabel ('Amplitude');
title ('saw tooth wave');
subplot(2,1,2);
plot(t,y);axis ( [ 0 0.1 -1 1 ] )
xlabel('time');
ylabel('amplitude');
saw tooth wave
1
Amplitude
0.5
0
-0.5
-1
0.01
0.02
0.03
0.04
0.05
Time
0.06
0.07
0.08
0.09
0.1
0.01
0.02
0.03
0.04
0.05
time
0.06
0.07
0.08
0.09
0.1
amplitude
0.5
0
-0.5
-1
Triangular signal
fs = 10000;
t = 0 : 1/fs : 1.5;
x = sawtooth (2 * pi * 50 * t , 0.5);
plot(t,x);
axis ( [ 0 0.1 -1 1 ] );
xlabel ('Time Index t (sec.)');
ylabel ('Amplitude');
title ('Triangular Wave Signal ');
Signal addition:
clc; clear all;
t = 0 : 0.01 : 30;
x1 = sin( 2 * pi * 1/3 * t );
x2 = sin( 2 * pi * 1/5 * t );
y = x1 + x2;
subplot( 3,1,1 ); plot( t , x1 );
grid;
xlabel( ' Time Index
t (sec.) ' );
ylabel( ' x1(t) ' );
title( ' Signal 1 : Sine Wave of Frequency 1/3 Hz
' );
subplot( 3,1,2 ); plot( t , x2 );
grid;
xlabel( ' Time Index t (sec.) ' );
ylabel( ' x2(t) ' );
title( ' Signal 2 : Sine Wave of Frequency 1/5 Hz
' );
subplot( 3,1,3 ); plot( t , y ); grid;
xlabel( ' Time Index t (sec.) ' );
ylabel( ' y(t) = x1(t) + x2(t) ' );
title( ' Resultant Signal : Signal 1 + Signal 2 ' );
1
0
-1
10
15
20
Time Index t (sec.)
Signal 2 : Sine Wave of Frequency 1/5 Hz
25
30
10
25
30
25
30
x2(t)
1
0
-1
15
20
Time Index t (sec.)
Resultant Signal : Signal 1 + Signal 2
2
0
-2
10
15
20
Time Index t (sec.)
Signal multiplication:
clc; clear all
t = 0 : 0.01 : 30;
x1 = sin( 2 * pi * 1/3 * t );
x2 = sin( 2 * pi * 1/5 * t );
y = x1 .* x2;
subplot( 3,1,1 ); plot( t , x1 ); grid;
xlabel( ' Time Index t (sec.) ' );
ylabel( ' x1(t) ' );
title( ' Signal 1 : Sine Wave of Frequency 1/3 Hz
' );
subplot( 3,1,2 ); plot( t , x2 ); grid;
xlabel( ' Time Index t (sec.) ' );
ylabel( ' x2(t) ' );
title( ' Signal 2 : Sine Wave of Frequency 1/5 Hz
' );
subplot( 3,1,3 ); plot( t , y ); grid;
xlabel( ' Time Index t (sec.) ' );
ylabel( ' y(t) = x1(t) .* x2(t) ' );
title( ' Resultant Signal : Dot Product of Signal 1 and Signal 2 ' );
1
0
-1
10
15
20
Time Index t (sec.)
Signal 2 : Sine Wave of Frequency 1/5 Hz
10
10
25
30
x2(t)
1
0
-1
15
20
25
Time Index t (sec.)
Resultant Signal : Dot Product of Signal 1 and Signal 2
30
1
0
-1
15
20
Time Index t (sec.)
25
30
Signal scaling:
clc; clear all
N = input ( ' Type the desired length of the signal
');
t = 0 : 0.01 : N-1;
x = sin( 2 * pi * 1/5 * t );
A = input ( ' Please input a SCALE FACTOR(>1 or a +ve fraction) for A
');
y = A * sin( 2 * pi * 1/5 * t );
subplot( 2,1,1 ); plot( t , x );
axis ( [ 0 N-1 -A A ] ); grid;
xlabel( ' Time Index t (sec.) ' );
ylabel( ' x(t) ' );
title( ' Signal 1 : Sine Wave of Frequency 1/5 Hz
' );
subplot( 2,1,2 ); plot( t , y );
axis ( [ 0 N-1 -A A ] ); grid;
xlabel( ' Time Index t (sec.) ' );
ylabel( ' y(t) ' );
title( ' Signal 2 : Scaled Version of Signal 1 ' );
x(t)
1
0
-1
-2
4
5
6
7
Time Index t (sec.)
Signal 2 : Scaled Version of Signal 1
10
10
y(t)
1
0
-1
-2
4
5
6
Time Index t (sec.)
original signal
amplitude
4
2
0
1.5
2.5
3
samples
even part of sequence
3.5
1.5
2.5
3
samples
odd part of sequence
3.5
1.5
3.5
amplitude
4
2
0
amplitude
2
0
-2
2.5
samples
Signal folding:
clc; clear all
t = 0 : 0.001 : 1;
x = 0.5 * t;
lx = length(x);
nx = 0 : lx-1;
xf = fliplr( x );
nf = -fliplr( nx );
subplot(2,1,1);
plot( nx , x );
xlabel( ' nx ' );
ylabel( ' x(nx) ' );
title( ' Original Signal
' );
subplot(2,1,2);
plot( nf , xf );
xlabel( ' nf ' );
ylabel( ' xf(nf) ' );
title( ' Folded Signal
' );
Original Signal
0.8
x(nx)
0.6
0.4
0.2
0
100
200
300
400
500
600
nx
Folded Signal
0
-1000
-900
-800
-700
-600
700
800
900
1000
-300
-200
-100
0.8
xf(nf)
0.6
0.4
0.2
-500
nf
-400
Output:
Enter x[n]: [1 2 3 4]
Enter h[n]: [1 2]
Z=
1 4 7 10 8
Input sequence x[n]
Amplitude
4
2
0
0.5
1.5
2
Time
Impulse response of the system h[n]
2.5
Amplitude
2
1
0
0.1
0.2
0.3
0.4
0.5
0.6
Time
Linear Convolution
0.7
0.8
0.9
Amplitude
10
5
0
0.5
1.5
2
Time
2.5
3.5
Output:
enter x[n]: [1 2 3 4]
Enter h[n]: [1 2]
xnew =
1
hnew =
1
xf =
10.0000
hf =
3.0000
zf =
30.0000
z=
10
Amplitude
Amplitude
Amplitude
0.5
1.5
2
time
Second sequence h(n)
2.5
2
1
0
0.1
0.2
0.3
0.4
0.5
0.6
time
Convolution
0.7
0.8
0.9
10
5
0
Auto correlation
0.5
1.5
2
time
2.5
3.5
20.0000
30.0000
20.0000
11.0000
4.0000
Amplitude
3
2
1
0
0.5
-2
-1
1.5
2
Time
Autocorrelation of x[n]
2.5
Amplitude
30
20
10
0
-3
Cross correlation
0
Time
Output:
Enter the first sequence x[n]: [1 2 3 4]
Enter the second sequence y[n]: [5 6 7 8]
Rxy =
8.0000 23.0000 44.0000 70.0000 56.0000 39.0000 20.0000
Amplitude
Amplitude
Amplitude
0.5
0.5
1.5
Time
Input sequence y[n]
2.5
2.5
10
5
0
1.5
2
Time
Cross correlation of x[n] and y[n]
100
50
0
-3
-2
-1
0
Time
clc;
clf;
close all;
ap=input('enter pass band ripple');
as=input('enter stop band ripple');
fp=input('enter pass band frequency');
fs=input('enter stop band frequency');
wp=2*pi*fp;
ws=2*pi*fs;
[n,wn]=buttord(wp,ws,ap,as,'s')
[b,a]=butter(n,wn,'low','s')
w=0:0.01*pi:1000*pi;
h=freqs(b,a,w);
m=abs(h);
a=angle(h);
subplot(2,1,1);
plot(w/pi,m);
xlabel('normalised frequency');
ylabel('m');
title('magnitude response');
subplot(2,1,2);
plot(w/pi,a);
xlabel('normalised frequency');
ylabel('a');
title('phase reponse');
Output:
enter pass band ripple 2
enter stop band ripple 10
1.6755
a =
1.0e+004 *
0.0001
1.6755
magnitude response
1.01
0.99
0.98
100
200
300
400
500
600
normalised frequency
phase reponse
700
800
900
1000
100
200
300
400
500
600
normalised frequency
700
800
900
1000
-0.05
-0.1
-0.15
-0.2
clf;
close all;
ap=input('enter pass band ripple');
as=input('enter stop band ripple');
fp=input('enter pass band frequency');
fs=input('enter stop band frequency');
wp=2*pi*fp;
ws=2*pi*fs;
[n,wn]=buttord(wp,ws,ap,as,'s')
[b,a]=butter(n,wn,'high','s')
w=0:0.01*pi:16000*pi;
h=freqs(b,a,w);
m=abs(h);
a=angle(h);
subplot(2,1,1);
plot(w/pi,m);
xlabel('normalised frequency');
ylabel('m');
title('magnitude response');
subplot(2,1,2);
plot(w/pi,a);
xlabel('normalised frequency');
ylabel('a');
title('phase response');
Output:
enter pass band ripple 2
enter stop band ripple 10
a =
1.0e+004 *
0.0001
3.7699
magnitude response
0.8
0.6
0.4
0.2
0
2000
4000
6000
8000
10000
normalised frequency
phase reponse
12000
14000
16000
2000
4000
6000
8000
10000
normalised frequency
12000
14000
16000
1.5
1
0.5
0
Decimation:
clc; clf; clear all;
D= input('Enter the Decimation factor= ');
t= 0:0.00025:1;
x=sin(2*pi*30*t) + sin(2*pi*60*t);
y=decimate(x,D,'fir');
subplot(2,1,1);stem(x(1:120));
axis([0 120 -2 2]);
title('original signal');
xlabel('time n');
ylabel('Amplitude');
subplot(2,1,2);stem(y(1:30));
title('Decimated signal');
xlabel('time n');
ylabel('Amplitude');
Output:
Enter the Decimation factor= 4
original signal
2
Amplitude
1
0
-1
-2
20
40
60
time n
Decimated signal
80
100
120
10
15
time n
20
25
30
Amplitude
1
0
-1
-2
Interpolation
t= 0:0.001:1;
x=sin(2*pi*30*t) + sin(2*pi*60*t);
y=interp(x,I);
subplot(2,1,1);stem(x(1:30));
title('original signal');
xlabel('time n');
ylabel('Amplitude');
subplot(2,1,2);stem(y(1:120));
title('Interpolated signal');
xlabel('time n');
ylabel('Amplitude');
Output:
Amplitude
1
0
-1
-2
10
15
time n
Interpolated signal
20
25
30
20
40
60
time n
80
100
120
Amplitude
1
0
-1
-2