Example:1: A.Circular Shift: All All

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 28

EXAMPLE:1:

a.CIRCULAR SHIFT

clc;
clear all;
close all;

x = [1 2 3 4 5];
n1 = [0 1 2 3 4];
subplot(2,1,1),stem(n1,x),title('main signal'),xlabel('n'),ylabel('x[n]')
N = length(x);
m = 2;
x = [x zeros(1,N-length(x))];
n = 0:N-1;
n = mod(n-m,N);
y = x(n+1);
subplot(2,1,2),stem(n1,y),title('2 pt circular shifted
signal'),xlabel('n'),ylabel('y[n]')

OUTPUT:
main signal
6

4
x[n]

0
0 0.5 1 1.5 2 2.5 3 3.5 4
n
2 pt circular shifted signal
6

4
y[n]

0
0 0.5 1 1.5 2 2.5 3 3.5 4
n

b. CIRCULAR CONVOLUTION:
clc;
clear all;
close all;

x1 = [1 2 2 0];
x2 = [1 2 3 4];
n = [0 1 2 3];
tic
y1= gallery('circul',x2)'*x1';
toc
tic
y2=ifft(fft(x1).*fft(x2));
toc
subplot(4,1,1),stem(n,x1),title('1st signal'),xlabel('n'),ylabel('x1[n]')
subplot(4,1,2),stem(n,x2),title('2nd signal'),xlabel('n'),ylabel('x2[n]')
subplot(4,1,3),stem(n,y1),title('Circular convolution using gallery
function'),xlabel('n'),ylabel('y1[n]')
subplot(4,1,4),stem(n,y2),title('Circular convolution using
fft'),xlabel('n'),ylabel('y2[n]')

OUTPUT:

Elapsed time is 0.034245 seconds.

Elapsed time is 0.000032 seconds.

1st signal
2
x1[n]

1
0
0 0.5 1 1.5 2 2.5 3
n
2nd signal
4
x2[n]

2
0
0 0.5 1 1.5 2 2.5 3
n
Circular convolution using gallery function
20
y1[n]

10
0
0 0.5 1 1.5 2 2.5 3
n
Circular convolution using fft
20
y2[n]

10
0
0 0.5 1 1.5 2 2.5 3
n

EXAMPLE:2:
LINEAR CONVOLUTION:
clc;
clear all;
close all;
%linear convolution using convolution matrix
h = [1 2 3]';
x = [1 2 2 1]';
tic
y = (convmtx(x,3)*h)';
toc
%linear convolution using N pt dft where N is the length of convolved
%result
h1=[1 2 3 0 0 0];
x2=[1 2 2 1 0 0];
tic
y1=ifft(fft(h1).*fft(x2));
toc
y
y1

OUTPUT:

Elapsed time is 0.166655 seconds.


Elapsed time is 0.000101 seconds.

y =

1 4 9 11 8 3

y1 =

1.0000 4.0000 9.0000 11.0000 8.0000 3.0000

EXAMPLE:3
AUTOCORRELATION USING TIME AND FREQUENCY DOMAIN TECHNIQUE:
clc;
clear all;
close all;

x = [1 1 0 1];
x1 = [x zeros(1,length(x)-1)];
X1 = fft(x1);
X1conj = conj(X1);
Rx = fftshift(ifft(X1.*X1conj))
rx = xcorr(x)

OUTPUT:

Rx =

1.0000 1.0000 1.0000 3.0000 1.0000 1.0000 1.0000

rx =
1 1 1 3 1 1 1

EXERCISE:3:
clc;
clear all;
close all;

x1=rand(1,51);
n=1:51;
x2=sin(2*pi*(1000)*n/10000);

x=x1+x2;
N=length(x);
nx=[x zeros(1,N-1)];
NX=fft(nx);
NXc=conj(NX);
Rx=fftshift(ifft(NX.*NXc));
rx=xcorr(x);
figure(1)
subplot(2,1,1),plot(Rx),title('Autocorrelation from time
domain'),xlabel('n'),ylabel('rx[n]');
subplot(2,1,2),plot(rx),title('Autocorrelation from frequency
domain'),xlabel('n'),ylabel('RX[n]');

P=(NX.*NXc)/51;
figure(2)
stem(P),title('PSD from frequency domain')

OUTPUT:
Autocorrelation from time domain

40

20
rx[n]

-20
0 20 40 60 80 100 120
n

Autocorrelation from frequency domain

40

20
RX[n]

-20
0 20 40 60 80 100 120
n

PSD from frequency domain


16

14

12

10

0
0 20 40 60 80 100 120

EXERCISE 4.1:
clc; clear all; close all;

N = 64;
ts = 125e-6;
fs = 1/ts;
f1 = 1062.5;
f2 = 1625;
t = (0:N-1)/fs;
x = sin(2*pi*f1*t) + 0.05*sin(2*pi*f2*t);

f=(0:N-1)/(N*ts)-fs/2;
figure(1)
subplot(2,1,1), plot(t,x),title('main signal'),xlabel('t'),ylabel('x');
subplot(2,1,2), stem(f,abs(fftshift(fft(x)))),title('magnitude
spectrum'),xlabel('f'),ylabel('X[f]');

w=hann(64);
nx=x.*w';
figure(2)
subplot(2,1,1), plot(t,nx),title('signal after
windowing'),xlabel('t'),ylabel('nx');
subplot(2,1,2), stem(f,abs(fftshift(fft(nx)))),title('magnitude
spectrum'),xlabel('f'),ylabel('NX[f]')

OUTPUT:

main signal
1.5

0.5

0
x

-0.5

-1

-1.5
0 1 2 3 4 5 6 7 8
t -3
10
magnitude spectrum
25

20

15
X[f]

10

0
-4000 -3000 -2000 -1000 0 1000 2000 3000 4000
f
signal after windowing
1

nx
0.5

-0.5

-1
0 1 2 3 4 5 6 7 8
t -3
10
magnitude spectrum

10
NX[f]

0
-4000 -3000 -2000 -1000 0 1000 2000 3000 4000
f

EXERCISE:4.3
clc;
clear all;
close all;

w0 = 0.2*pi;
w1 = 0.22*pi;
w2 = 0.6*pi;
N = 1024;
n = 0:100;
x = cos(w0*n) + cos(w1*n) + cos(w2*n);

fn=linspace(-1,1-1/N,N);
x1=x(1:25);
x2=x(1:50);
x3=x(1:100);

figure(1)
subplot(3,1,1), plot(fn,abs(fftshift(fft(x1,N)))),title('magnitude spectrum
using 25 pt signal'),xlabel('f'),ylabel('X1[f]');

subplot(3,1,2), plot(fn,abs(fftshift(fft(x2,N)))),title('magnitude spectrum


using 50 pt signal'),xlabel('f'),ylabel('X2[f]');
subplot(3,1,3), plot(fn,abs(fftshift(fft(x3,N)))),title('magnitude spectrum
using 100 pt signal'),xlabel('f'),ylabel('X3[f]');

X1=x1'.*(hann(25));
X2=x2'.*(hann(50));
X3=x3'.*(hann(100));

figure(2)
subplot(3,1,1), plot(fn,abs(fftshift(fft(X1,N)))),title('magnitude spectrum
after windowing'),xlabel('f'),ylabel('X1[f]');
subplot(3,1,2), plot(fn,abs(fftshift(fft(X2,N)))),title('magnitude spectrum
after windowing'),xlabel('f'),ylabel('X2[f]');
subplot(3,1,3), plot(fn,abs(fftshift(fft(X3,N)))),title('magnitude spectrum
after windowing'),xlabel('f'),ylabel('X3[f]');
OUTPUT:

magnitude spectrum using 25 pt signal


30

20
X1[f]

10

0
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
f
magnitude spectrum using 50 pt signal
40

30
X2[f]

20

10

0
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
f
magnitude spectrum using 100 pt signal
60

40
X3[f]

20

0
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
f
magnitude spectrum after windowing
15

10
X1[f]

0
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
f
magnitude spectrum after windowing
20

15
X2[f]

10

0
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
f
magnitude spectrum after windowing
30

20
X3[f]

10

0
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
f

EXERCISE :5.1:
MODULATION USING DIFFERENT LENGTH OF MESSAGE SIGNAL:
clc;
clear all;
close all;

%Modulation
to = .01;
fs =1250;
fc = 250;
ts = 1/fs;
t = [-to:ts:to];
m = sinc(100*t);
c = cos(2*pi*fc*t);
u = m.*c;
N = 1024;
M = fft(m,1024);
M = M/fs;
U = fft(u,1024);
U = U/fs;
fn = [0:1/N:1-1/N]*fs - fs/2;
figure(1)
subplot(2,1,1),plot(fn,abs(fftshift(M))),title('Spectra of message signal
using t0=.01sec');
subplot(2,1,2),plot(fn,abs(fftshift(U))),title('Spectra of modulated signal
using t0=.01sec');

OUTPUT:

Spectra of message signal using t0=.01sec


0.012

0.01

0.008

0.006

0.004

0.002

0
-800 -600 -400 -200 0 200 400 600 800

10 -3 Spectra of modulated signal using t0=.01sec


6

0
-800 -600 -400 -200 0 200 400 600 800

clc;
clear all;
close all;

%Modulation
to = .1;
ts = .001;
fc = 250;
fs = 1/ts;
t = [-to:ts:to];
m = sinc(100*t);
c = cos(2*pi*fc*t);
u = m.*c;
N = 1024;
M = fft(m,1024);
M = M/fs;
U = fft(u,1024);
U = U/fs;
fn = [0:1/N:1-1/N]*fs - fs/2;
figure(1)
subplot(2,1,1),plot(fn,abs(fftshift(M))),title('Spectra of message signal
using t0=.1sec');
subplot(2,1,2),plot(fn,abs(fftshift(U))),title('Spectra of modulated signal
using t0=.1sec');

OUTPUT:

Spectra of message signal using t0=.1sec


0.012

0.01

0.008

0.006

0.004

0.002

0
-500 -400 -300 -200 -100 0 100 200 300 400 500

10 -3 Spectra of modulated signal using t0=.1sec


6

0
-500 -400 -300 -200 -100 0 100 200 300 400 500

HOMEWORK:
EXTRACT x=sin(2*pi*f1*t) from x=sin(2*pi*f1*t)+ .5 sin(2*pi*f1*t)
clc; clear all; close all;

f1 = 500;
f2 = 1000;
fs = 8000;
ts = 1/fs;
N=2*(fs/gcd(f1,f2));
t = (0:N-1)*ts;
x = sin(2*pi*f1*t) + 0.5*sin(2*pi*f2*t);
f = ((0:length(t)-1)/(length(t)*ts))-1/(2*ts);
X=fft(x);
figure(1)
stem(t,x),title('main signal')
figure(2)
stem(f,abs(fftshift(X))),title('magnitude spectra of main signal');

fcut = 800;
ncut = floor(fcut*N/fs);
H = zeros(1,N);
H(1:ncut) = ones(1,ncut);
H(N-ncut+1:N) =ones(1,ncut);
U = X.*H;
figure(3)
stem(f,abs(fftshift(U))),title('magnitude spectra of main signal after using
low pass filter');

figure(4);
subplot(2,1,1),stem(sin(2*pi*f1*t)),title('1st part of main signal');
subplot(2,1,2),stem(ifft(U)),title('1st part of main signal after using low
pass filter and ifft function');

OUTPUT:

main signal
1.5

0.5

-0.5

-1

-1.5
0 0.5 1 1.5 2 2.5 3 3.5 4
10 -3
magnitude spectra of main signal
16

14

12

10

0
-4000 -3000 -2000 -1000 0 1000 2000 3000 4000

magnitude spectra of main signal after using low pass filter


16

14

12

10

0
-4000 -3000 -2000 -1000 0 1000 2000 3000 4000
1st part of main signal
1

0.5

-0.5

-1
0 5 10 15 20 25 30 35

1st part of main signal after using low pass filter and ifft function
1

0.5

-0.5

-1
0 5 10 15 20 25 30 35

EXERCISE 5.2:
FREQUENCY DIVISION MULTIPLEXING:
clc;
clear all;
close all;

to = .1;
fc1 = 250;
fc2 = 750;
fs = 2000;
ts=1/fs;
t = [-to:ts:to];
m1 = sinc(100*t);
m2 = sinc(100*t).^2;
c1 = cos(2*pi*fc1*t);
c2 = cos(2*pi*fc2*t);
u = m1.*c1 + m2.*c2;
figure(1)
plot(t,u),title('modulated signal');

N = 1024;
M1 = fft(m1,1024);
M1 = M1/fs;
M2 = fft(m2,1024);
M2 = M2/fs;
U = fft(u,1024);
U = U/fs;
fn = [0:1/N:1-1/N]*fs-fs/2 ;
figure(2)
plot(fn,abs(fftshift(M1))),title('magnitude spectra of 1st signal');
figure(3)
plot(fn,abs(fftshift(M2))),title('magnitude spectra of 2nd signal');
figure(4)
plot(fn,abs(fftshift(U))),,title('magnitude spectra of modulated signal');
%demodulation
fcut = 450;
ncut = floor(fcut*N/fs);

H1 = zeros(1,N);
H1(1:ncut) = ones(1,ncut);
H1(N-ncut+1:N) = ones(1,ncut);
y1=U.*H1;
figure(5)
plot(fn,abs(fftshift(y1))),title('magnitude spectra of modulated signal after
using 1ST BPF');
H2 = ones(1,N);
H2(1:ncut) = 0;
H2(N-ncut+1:N) =0;
y2 = U.*H2;
figure(6)
plot(fn,abs(fftshift(y2))),title('magnitude spectra of modulated signal after
using 2nd BPF');

r1 = m1.*(c1.^2);
R1 = fft(r1,1024);
R1 = R1/fs;
figure(7);
plot(fn,abs(fftshift(R1))),title('magnitude spectra after modulating the
output of 1ST BPF');
fcut = 200;
ncut = floor(fcut*N/fs);
H3 = zeros(1,N);
H3(1:ncut) = 2*ones(1,ncut);
H3(N-ncut+1:N) = 2*ones(1,ncut);
S1 =(R1).*H3;
figure(8);
plot(fn,abs(fftshift(S1))),title('magnitude spectra of demodulated signal
after using 1ST LPF');

r2 = m2.*(c2.^2);
R2 = fft(r2,1024);
R2 = R2/fs;
figure(9);
plot(fn,abs(fftshift(R2))),title('magnitude spectra after modulating the
output of 2nd BPF');
S2 =(R2).*H3;
figure(10);
plot(fn,abs(fftshift(S2))),title('magnitude spectra of demodulated signal
after using 2nd LPF');

figure(11);
stem(fftshift(ifft((S1)))),title('demodulated 1st signal');

figure(12);
stem(fftshift(ifft(S2))),title('demodulated 2nd signal');
OUTPUT:

modulated signal
2

1.5

0.5

-0.5

-1

-1.5

-2
-0.1 -0.08 -0.06 -0.04 -0.02 0 0.02 0.04 0.06 0.08 0.1

magnitude spectra of 1st signal


0.012

0.01

0.008

0.006

0.004

0.002

0
-1000 -800 -600 -400 -200 0 200 400 600 800 1000
magnitude spectra of 2nd signal
0.01

0.009

0.008

0.007

0.006

0.005

0.004

0.003

0.002

0.001

0
-1000 -800 -600 -400 -200 0 200 400 600 800 1000
magnitude
10 -3 spectra of modulated signal after using 1ST BPF
6

0
-1000 -800 -600 -400 -200 0 200 400 600 800 1000
10-4 demodulated 1st signal
5

-1

-2
0 200 400 600 800 1000 1200

10 -4 demodulated 2nd signal


6

-1
0 200 400 600 800 1000 1200
INSIGTFUL EXERCISE 1:
clc;
close all;
clear all;

N = 1000;
w = randn(1,N);
figure(1);
plot(w);

[rw,lags] = xcorr(w);
figure(2)
plot(lags,rw);

W1 = fft(rw);

psd = 10*log10(W1);
figure(3)
stem(psd)

OUTPUT:

-1

-2

-3

-4
0 100 200 300 400 500 600 700 800 900 1000
1000

800

600

400

200

-200
-1000 -800 -600 -400 -200 0 200 400 600 800 1000

40

35

30

25

20

15

10

-5

-10
0 200 400 600 800 1000 1200 1400 1600 1800 2000
INSIGHTFUL EXERCISE 3:
clc;
clear all;
close all;

w = [0 pi/3 2*pi/3 pi];


L = 2;
f = w/pi;
mag = [1 1 0 0];
x = fir2(99,f,mag);
[X,w] = freqz(x,1,512);
figure(1)
plot(w/pi,abs(X))
figure(2)
stem(x)

y = zeros(1,L*length(x));
y(1:L:length(y)) = x;
figure(3)
stem(y)
[Y,w] = freqz(y,1,512);
figure(4)
plot(w/pi,abs(Y))

syms n w L a;
ym = sin(a*n);
Ym = ztrans(ym)
ym2 = sin(a*n/L);
Ym2 = ztrans(ym2)

OUTPUT:

Ym =

(z*sin(a))/(z^2 - 2*cos(a)*z + 1)

Ym2 =

(z*sin(a/L))/(z^2 - 2*cos(a/L)*z + 1)
1.2

0.8

0.6

0.4

0.2

0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

0.5

0.4

0.3

0.2

0.1

-0.1
0 10 20 30 40 50 60 70 80 90 100
0.5

0.4

0.3

0.2

0.1

-0.1
0 20 40 60 80 100 120 140 160 180 200

1.2

0.8

0.6

0.4

0.2

0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

You might also like