You are on page 1of 24

Unit step:

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');

discrete unit step sequence

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');

unit impulse sequence


1
0.9
0.8
0.7

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

positive ramp signal


10

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 ' );

Signal 1 : Sine Wave of Frequency 1/3 Hz


x1(t)

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

y(t) = x1(t) + x2(t)

-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 ' );

Signal 1 : Sine Wave of Frequency 1/3 Hz


x1(t)

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

y(t) = x1(t) .* x2(t)

-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 ' );

Signal 1 : Sine Wave of Frequency 1/5 Hz


2

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.)

Even and odd parts of signal:


clc;
clear all;
close all;
x=input('enter sequence');
n=length(x);
fliplr(x)
xe=0.5*(x+fliplr(x))
xo=0.5*(x-fliplr(x))
subplot(3,1,1);
stem(x);
xlabel('samples');
ylabel('amplitude');
title('original signal');
subplot(3,1,2);
stem(xe);
xlabel('samples');
ylabel('amplitude');
title('even part of sequence');
subplot(3,1,3);
stem(xo);
xlabel('samples');
ylabel('amplitude');
title('odd part of sequence');

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

Convolution in time domain

-500
nf

-400

clc;clear all;close all;


x=input('Enter x[n]:');
nx=0:length(x)-1;
h=input('Enter h[n]:');
nh=0:length(h)-1;
z=conv(x,h);
nz=0:length(z)-1;
subplot(3,1,1);
stem(nx,x);
xlabel('Time');ylabel('Amplitude');
title('Input sequence x[n]');
subplot(3,1,2);
stem(nh,h);
xlabel('Time');ylabel('Amplitude');
title('Impulse response of the system h[n]');
subplot(3,1,3);
stem(nz,z);
xlabel('Time');ylabel('Amplitude');
title('Linear Convolution');

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

Convolution in frequency domain:


clc;clear all;close all;
x=input('enter x[n]:');
h=input('Enter h[n]:');
nx=length(x);
nh=length(h);
n=nx+nh-1;
xnew=[x,zeros(1,n-nx)]
hnew=[h,zeros(1,n-nh)]
xf=fft(xnew)
hf=fft(hnew)
zf=xf.*hf
z=ifft(zf)
lx=0:1:nx-1;
lh=0:1:nh-1;
nz=0:1:length(z)-1;
subplot(3,1,1);
stem(lx,x);
xlabel('time');
ylabel('Amplitude');
title('First sequence x(n)');
subplot(3,1,2);
stem(lh,h);
xlabel('time');
ylabel('Amplitude');
title('Second sequence h(n)')
subplot(3,1,3);
stem(nz,z);
xlabel('time');
ylabel('Amplitude');
title('Convolution')

Output:

enter x[n]: [1 2 3 4]
Enter h[n]: [1 2]
xnew =
1

hnew =
1
xf =
10.0000

-4.0451 - 1.3143i 1.5451 - 2.1266i 1.5451 + 2.1266i -4.0451 + 1.3143i

hf =
3.0000

1.6180 - 1.9021i -0.6180 - 1.1756i -0.6180 + 1.1756i 1.6180 + 1.9021i

zf =
30.0000
z=

-9.0451 + 5.5676i -3.4549 - 0.5020i -3.4549 + 0.5020i -9.0451 - 5.5676i


4

10

Amplitude

Amplitude

Amplitude

First sequence x(n)


4
2
0

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

clc; clear all;


x=input(Enter x[n]:);
n=0:1:length(X)-1;
subplot(2,1,1);
stem(n,x);
xlabel(Time);
ylabel(Amplitude);
title(Input sequence x[n])
Rxx = xcorr(x); disp(Rxx);
nRxx = -length(x)+1:length(x)- 1;
subplot(2,1,2);
stem(nRxx,Rxx);
xlabel(Time);
ylabel(Amplitude);
title(Autocorrelation of x(n));
Output:
Enter x[n] : [1 2 3 4]
Rxx =
4.0000 11.0000

20.0000

30.0000

20.0000

11.0000

4.0000

Input sequence x[n]


4

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

clc;clear all;close all;


x=input('Enter the first sequence x[n]:');
y=input('Enter the second sequence y[n]:');
nx=length(x);
ny=length(y);
n=max(nx,ny);
Rxy=xcorr(x,y);
nxl=0:1:nx-1;
subplot(3,1,1); stem(nxl,x);
xlabel('Time');ylabel('Amplitude');
title('Input sequence x[n]');
subplot(3,1,2);stem(nyl,y);
xlabel('Time');ylabel('Amplitude');
title('Input sequence y[n]');
l=-n+1:n-1;
subplot(3,1,3); stem(l,Rxy);
xlabel('Time');ylabel('Amplitude');
title('Cross correlation of x[n] and y[n]');

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

Input sequence x[n]


4
2
0

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

Butterworth low pass filter

-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

enter pass band frequency 2000


enter stop band frequency 8000
n =
1
wn =
1.6755e+004
b =
1.0e+004 *
0

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

Butterworth high pass filter


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,'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

enter pass band frequency 8000


enter stop band frequency 2000
n =
1
wn =
3.7699e+004
b =
1

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

clc; clf; clear all;


I= input('Enter the interpolation factor= ');

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:

Enter the interpolation factor= 4


original signal
2

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

You might also like