0% found this document useful (0 votes)
66 views53 pages

DSP LAB Manual

The document describes an experiment to calculate the autocorrelation of a given sequence and verify its properties in MATLAB. Autocorrelation measures the similarity between a signal and a time-shifted version of itself over different time lags. It is used to identify repeating patterns within a signal, such as the presence of a periodic signal which may be obscured by noise. The experiment involves calculating the autocorrelation of a sequence in MATLAB and verifying that it has the following properties: even symmetry, maximum value at zero lag, and decreasing values as the time lag increases.

Uploaded by

Akshay Deolasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views53 pages

DSP LAB Manual

The document describes an experiment to calculate the autocorrelation of a given sequence and verify its properties in MATLAB. Autocorrelation measures the similarity between a signal and a time-shifted version of itself over different time lags. It is used to identify repeating patterns within a signal, such as the presence of a periodic signal which may be obscured by noise. The experiment involves calculating the autocorrelation of a sequence in MATLAB and verifying that it has the following properties: even symmetry, maximum value at zero lag, and decreasing values as the time lag increases.

Uploaded by

Akshay Deolasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

Experiment 1

Name – Akshay Deolasi

MIS – 112107011

Batch – A

Subject – Digital Signal Processing

1) Sine Wave

Code -

N = 1024;

fs = 200;

f = 1;

ts = 1/fs;

t = ts*(0:N-1);

x = sin(2*pi*f*t);

plot(t,x);

Output –
2) Cosine Wave

Code -

N = 1024;

fs = 200;

f = 1;

ts = 1/fs;

t = ts*(0:N-1);

x = cos(2*pi*f*t);

plot(t,x);

Output –

3) Discrete Cosine Wave

Code -

N = 1024;

fs = 200;

f = 1;

ts = 1/fs;
t = ts*(0:N-1);

x = cos(2*pi*f*t);

stem(t,x);

Output –

Code -

N = 100;

fs = 200;

f = 1;

ts = 1/fs;

t = ts*(0:N-1);

x = cos(2*pi*f*t);

stem(t,x);
Output –

4) Discrete Sine Wave

Code –

N = 1024;

fs = 200;

f = 1;

ts = 1/fs;

t = ts*(0:N-1);

x = sin(2*pi*f*t);

stem(t,x);
Output –

Code –

N = 100;

fs = 200;

f = 1;

ts = 1/fs;

t = ts*(0:N-1);

x = sin(2*pi*f*t);

stem(t,x);
Output –

5) Impulse Function

Code -

t=(-1:1:1)';

impulse=t==0;

unistep=t>=0;

ramp=t.*unistep;

quad=t.^2.*unistep;

plot(t,[impulse])
Output –

6) Unit Step Function

Code -

t=(-1:1:1)';

impulse=t==0;

unistep=t>=0;

ramp=t.*unistep;

quad=t.^2.*unistep;

stem(t,[impulse])
Output –

7) Ramp Function

Code -

t=(-1:1:1)';

impulse=t==0;

unistep=t>=0;

ramp=t.*unistep;

quad=t.^2.*unistep;

plot(t,[ramp])
Output –

8) Quadratic Function

Code –

t=(-1:01:1)';

impulse=t==0;

unistep=t>=0;

ramp=t.*unistep;

quad=t.^2.*unistep;

plot(t,[quad])
Output –

Codes in Python :-

1) Sine Wave
i) Frequency = 1Hz

Code –

N = 1024

fs = 200

f=1

ts = 1 / fs

t = ts * np.arange(0, N)

x = np.sin(2 * np.pi * f * t)

plt.plot(t, x)

plt.xlabel('Time (s)')

plt.ylabel('Amplitude')
plt.title('Sine Wave Signal')

plt.show()

Output –

ii) Frequency = 10 Hz

Code –

N = 1024

fs = 200

f = 10

ts = 1 / fs

t = ts * np.arange(0, N)

x = np.sin(2 * np.pi * f * t)

plt.plot(t, x)

plt.xlabel('Time (s)')
plt.ylabel('Amplitude')

plt.title('Sine Wave Signal')

plt.show()

Output –

iii) Frequency = 100 Hz

Code –

N = 1024

fs = 200

f = 100

ts = 1 / fs

t = ts * np.arange(0, N)

x = np.cos(2 * np.pi * f * t)

plt.plot(t, x)
plt.xlabel('Time (s)')

plt.ylabel('Amplitude')

plt.title('Sine Wave Signal')

plt.show()

Output –

2) Cosine Wave
i) Frequency = 1Hz

Code –

import numpy as np

import matplotlib.pyplot as plt

N = 1024

fs = 200

f=1
ts = 1 / fs

t = ts * np.arange(0, N)

x = np.cos(2 * np.pi * f * t)

plt.plot(t, x)

plt.xlabel('Time (s)')

plt.ylabel('Amplitude')

plt.title('Cosine Wave Signal')

plt.show()

Output –

ii) Frequency = 10 Hz

Code –

N = 1024

fs = 200
f = 10

ts = 1 / fs

t = ts * np.arange(0, N)

x = np.cos(2 * np.pi * f * t)

plt.plot(t, x)

plt.xlabel('Time (s)')

plt.ylabel('Amplitude')

plt.title('Cosine Wave Signal')

plt.show()

Output –

iii) Frequency = 100 Hz

Code –
N = 100

fs = 200

f = 100

ts = 1 / fs

t = ts * np.arange(0, N)

x = np.cos(2 * np.pi * f * t)

plt.plot(t, x)

plt.xlabel('Time (s)')

plt.ylabel('Amplitude')

plt.title('Cosine Wave Signal')

plt.show()

Output –
3) Discrete Cosine Wave

Code –

import numpy as np

import matplotlib.pyplot as plt

N = 1024

fs = 200

f=1

ts = 1 / fs

t = ts * np.arange(0, N)

x = np.cos(2 * np.pi * f * t)

plt.stem(t, x, use_line_collection=True)

plt.xlabel('Time (s)')

plt.ylabel('Amplitude')

plt.title('Discrete Cosine Wave Signal')

plt.show()
Output –

Code –

import numpy as np

import matplotlib.pyplot as plt

N = 1024

fs = 200

f=1

ts = 1 / fs

t = ts * np.arange(0, N)

x = np.cos(2 * np.pi * f * t)

plt.stem(t, x, use_line_collection=True)

plt.xlabel('Time (s)')
plt.ylabel('Amplitude')

plt.title('Discrete Cosine Wave Signal')

plt.show()

Output –

4) Discrete Sine Wave

Code –

import numpy as np

import matplotlib.pyplot as plt

N = 1024

fs = 200

f=1

ts = 1 / fs

t = ts * np.arange(0, N)
x = np.sin(2 * np.pi * f * t)

plt.stem(t, x, use_line_collection=True)

plt.xlabel('Time (s)')

plt.ylabel('Amplitude')

plt.title('Discrete Sine Wave Signal')

plt.show()

Output –

Code –

import numpy as np

import matplotlib.pyplot as plt

N = 1024

fs = 200
f=1

ts = 1 / fs

t = ts * np.arange(0, N)

x = np.sin(2 * np.pi * f * t)

plt.stem(t, x, use_line_collection=True)

plt.xlabel('Time (s)')

plt.ylabel('Amplitude')

plt.title('Discrete Sine Wave Signal')

plt.show()

Output –

5) Impulse Function, Unit Step Signal, Ramp Signal and Quadratic Function

Code –

import numpy as np
import matplotlib.pyplot as plt

t = np.arange(-1, 2, 1)

impulse = t == 0

unistep = t >= 0

ramp = t * unistep

quad = t**2 * unistep

plt.figure(figsize=(10, 6))

plt.subplot(2, 2, 1)

plt.plot(t, impulse)

plt.xlabel('Time')

plt.ylabel('Amplitude')

plt.title('Impulse Signal')

plt.subplot(2, 2, 2)

plt.plot(t, unistep)

plt.xlabel('Time')

plt.ylabel('Amplitude')

plt.title('Unit Step Signal')

plt.subplot(2, 2, 3)

plt.plot(t, ramp)

plt.xlabel('Time')
plt.ylabel('Amplitude')

plt.title('Ramp Signal')

plt.subplot(2, 2, 4)

plt.plot(t, quad)

plt.xlabel('Time')

plt.ylabel('Amplitude')

plt.title('Quadratic Signal')

plt.tight_layout()

plt.show()

Output –
CODE1:Sampling theorem

import numpy as np
import matplotlib.pyplot as plt

plt.clf()

T = 0.04
t = np.arange(0, 0.02, 0.0005)
f=1/T
nl = np.arange(0, 41)
print(np.size(nl))
xa_t = np.sin(2 * np.pi * 2 * t / T)

plt.subplot(2, 2, 1)
plt.plot(200 * t, xa_t)
plt.title('Verification of sampling theorem')
plt.title('Continuous signal')
plt.xlabel('t')
plt.ylabel('x(t)')

ts1 = 0.002
ts2 = 0.01
ts3 = 0.1

n = np.arange(0, 21)
x_ts1 = 2 * np.sin(2 * np.pi * n * ts1 / T)

plt.subplot(2, 2, 2)
plt.stem(n, x_ts1)
plt.title('Greater than Nyquist rate')
plt.xlabel('n')
plt.ylabel('x(n)')

n = np.arange(0, 5)
x_ts2 = 2 * np.sin(2 * np.pi * n * ts2 / T)

plt.subplot(2, 2, 3)
plt.stem(n, x_ts2)
plt.title('Equal to Nyquist rate')
plt.xlabel('n')
plt.ylabel('x(n)')

n = np.arange(0, 11)
x_ts3 = 2 * np.sin(2 * np.pi * n * ts3 / T)

plt.subplot(2, 2, 4)
plt.stem(n, x_ts3)
plt.title('Less than Nyquist rate')
plt.xlabel('n')
plt.ylabel('x(n)')

plt.tight_layout()
plt.show()
CODE2:Sine wave

import numpy as np
import matplotlib.pyplot as plt

plt.clf()
plt.close('all')

Fs = 150
f=5
t = np.arange(0, 1, 1/Fs)
x = np.sin(2 * np.pi * t * f)

nfft = 1024
X = np.fft.fft(x, nfft)
X = X[:nfft//2]
mx = np.abs(X)

frequencies = np.arange(0, nfft//2) * Fs / nfft

plt.figure(1)
plt.plot(t, x)
plt.title('Sine Wave Signal')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')

plt.figure(2)
plt.plot(frequencies, mx)
plt.title('Power Spectrum of a Sine Wave')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Power')

plt.show()
CODE3:Cosine wave

import numpy as np
import matplotlib.pyplot as plt

plt.clf()
plt.close('all')

Fs = 150
f=5
t = np.arange(0, 1, 1/Fs)
nfft = 1024
x = np.sin(2 * np.pi * t * f)

X = np.fft.fft(x, nfft)
X = X[:nfft//2]
mx = np.abs(X)

frequencies = np.arange(0, nfft//2) * Fs / nfft

plt.figure(1)
plt.plot(t, x)
plt.title('Sine Wave Signal')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')

plt.figure(2)
plt.plot(frequencies, mx)
plt.title('Power Spectrum of a Sine Wave')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Power')

plt.show()
CODE4:Square wave

import numpy as np
import matplotlib.pyplot as plt

Fs = 150
t = np.arange(0, 1, 1/Fs)
f=5
x = np.square(2 * np.pi * t * f)
nfft = 1024
X = np.fft.fft(x, nfft)
X = X[:nfft//2]
mx = np.abs(X)
frequencies = np.arange(0, nfft//2) * Fs / nfft

plt.figure(1)
plt.plot(t, x)
plt.title('Square Wave Signal')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')

plt.figure(2)
plt.title('Power Spectrum of a Square Wave')
plt.plot(frequencies, mx)
plt.xlabel('Frequency (Hz)')
plt.ylabel('Power')

plt.show()
CODE5:Guassian pulse

import numpy as np
import matplotlib.pyplot as plt

Fs = 60
t = np.arange(-5, 0.51, 1/Fs)
sigma = 0.1
x = 1 / (np.sqrt(2 * np.pi * sigma**2)) * np.exp(-t**2 / (2 * sigma**2))

nfft = 1024
X = np.fft.fft(x, nfft)
mx = np.abs(X)

f = np.arange(0, nfft//2) * Fs / nfft

plt.figure(1)
plt.plot(t, x)
plt.title('Gaussian Pulse Signal')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')

plt.figure(2)
plt.plot(f, mx[:nfft//2])
plt.title('Power Spectrum of a Gaussian Pulse')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Power')

plt.show()
CODE6:Exponential decay

import numpy as np
import matplotlib.pyplot as plt

Fs = 150
t = np.arange(0, 1.001, 1/Fs)
x = 2 * np.exp(-5 * t)

nfft = 1024
X = np.fft.fft(x, nfft)
X = X[:nfft//2] # FFT is symmetric, take only the first half
mx = np.abs(X)

f = np.arange(0, nfft//2) * Fs / nfft

plt.figure(1)
plt.plot(t, x)
plt.title('Exponential Decay Signal')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')

plt.figure(2)
plt.plot(f, mx)
plt.title('Power Spectrum of an Exponential Decay')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Power')

plt.show()
CODE7:Chirp signal

import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import chirp

Fs = 150
t = np.arange(0, 1.001, 1/Fs)
x = chirp(t, f0=0, f1=1, t1=1, f0_mode='logarithmic')

nfft = 1024
X = np.fft.fft(x, nfft)
X = X[:nfft//2] # FFT is symmetric, take only the first half
mx = np.abs(X)

f = np.arange(0, nfft//2) * Fs / nfft

plt.figure(1)
plt.plot(t, x)
plt.title('Chirp Signal')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')

plt.figure(2)
plt.plot(f, mx)
plt.title('Power Spectrum of a Chirp Signal')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Power')

plt.show()
EXPERIMENT NO. 03

Aim:Autocorrelation of a given sequence and verification of its properties. Software


Required: MATLAB

Theory:
The autocorrelation function of a random signal describes the general dependence of the
values of the samples at one time on the values of the samples at another time. Consider a random
process x(t) (i.e. continuous-time), its autocorrelation function is written as:

Where T is the period of observation.


Rxx () _ is always real-valued and an even function with a maximum value at 0.
For sampled signal (i.e. sampled signal), the autocorrelation is defined as either biased or unbiased
defined as follows:

[Biased Autocorrelation]

(2)

[Uniased Autocorrelation]

where M is the number of lags.


The cross correlation function however measures the dependence of the values of one
signal on another signal. For two WSS (Wide Sense Stationary) processes x(t) and
y(t) it is described by:

= lim
+ T)dt (3)

(4)
1f + c)dt
22
ET 20006: Lab Digital Signal Processing
where T is the observation time.
For sampled signals, it is defined as:

Where N is the record length (i.e. number of samples).


Program:
Autocorrelation of a given sequence and verification of its properties.
% Read the signal

% define the axis


n—0: 1
:length(x)-l %
plot the signal
stem(n,x);
xlabel('n');
% auto correlate the signal
Rxx=xcorr(x,x);
% the axis for auto correlation results
nRxx=-length(x)+1 :length(x)- I %
display the result stem(nRxx,Rxx)
% properties of Rxx(0) gives the energy of the signal
% find energy of the signal energy=sum(x.A2)
%set index of the centre value
centre_index—
ceil(length(Rxx)/2) % Acces the
centre value Rxx(0)
Rxx 0==Rxx(centre_index)
Rxx O—Rxx(centre_index)
% Check if the Rxx(0)=energy if Rxx
O==energy disp('Rxx(0) gives energy
proved'); else disp('Rxx(0) gives energy not
proved'); end Rxx_right=Rxx(centre_index: I
:length(Rxx))
Rxx
ifRxx right==Rxx_left disp('Rxx is
even'); else disp('Rxx is not
even'); end

23
ET 20006: Lab Digital Signal Processing
import numpy as np
import matplotlib.pyplot as plt
n = np.arange(0, len(x))
plt.stem(n, x)
Rxx = np.correlate(x, x, mode='full')
nRxx = np.arange(-len(x) + 1, len(x))
plt.stem(nRxx, Rxx)
energy = np.sum(x**2)
centre_index = len(Rxx) // 2
Rxx_0 = Rxx[centre_index]
if Rxx_0 == energy:
print('Rxx(0) gives energy proved')
else:
print('Rxx(0) gives energy not proved')
Rxx_right = Rxx[centre_index::]
Rxx_left = Rxx[:centre_index + 1]
if np.array_equal(Rxx_right, Rxx_left):
print('Rxx is even')
else:
print('Rxx is not even')

output:
x= 1 2 3 6 5 4
11=0 1 2 3 4 5 nRxx= -5 -4 -3 -2 -
1 0 1 2 3 4 5 energy = 91 centre
index = 6 Rxx(0) gives energy
not proved
Rxx_right =
91.0000 76.0000 54.0000 28.0000 13.0000 4.0000
Rxx left —
91.0000 76.0000 54.0000 28.0000 13.0000 4.0000
Rxx is even

24
ET 20006: Lab Digital Signal Processing
Autocorrelation of a sinewave
Plot the autocorrelation sequence of a sinewave with frequency 1 Hz, sampling frequency of 200
Hz.
N = 1024; % Number of samples
f = 1; % Frequency of the sine wave
FS = 200; % Sampling Frequency
n = 0:N-1; % Sample index numbers
x = sin(2 * pi * f * n / FS); % Generate the signal, x(n)

t = (1:N) * (1 / FS); % Prepare a time axis

% Prepare the figure and plot x(n)


subplot(2, 1, 1);
plot(t, x);
title('Sine wave of frequency 1000Hz [FS=8000Hz]');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;

% Estimate its autocorrelation


Rxx = xcorr(x);

% Prepare the figure and plot the autocorrelation


subplot(2, 1, 2);
plot(Rxx);
title('Autocorrelation function of the sine wave');

25
ET 20006: Lab Digital Signal Processing
xlabel('Lags');
ylabel('Autocorrelation');
grid on;

python code:
import numpy as np
import matplotlib.pyplot as plt

N = 1024
f=1
FS = 200
n = np.arange(N)
x = np.sin(2 * np.pi * f * n / FS)

t = (np.arange(1, N + 1) / FS)

plt.subplot(2, 1, 1)
plt.plot(t, x)
plt.title('Sine wave of frequency 1000Hz [FS=8000Hz]')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')
plt.grid()

Rxx = np.correlate(x, x, mode='full')

plt.subplot(2, 1, 2)
plt.plot(Rxx)
plt.title('Autocorrelation function of the sine wave')
plt.xlabel('Lags')
plt.ylabel('Autocorrelation')
plt.grid()

plt.tight_layout()
plt.show()

26
ET 20006: Lab Digital Signal Processing
output:

o 6
Tin-re, [s]
5
Autocorrelation fiån•ction of the sin—wave
1000

o 500 1000 soo


2500
lags
Cross correlation

Plot the cross correlation of the following


signal: x(n)— sin(211flt ) with fl Hz y(n)=
w(n)

whelæw(n) is a zeros mean, unit variance of Gaussina random process.

N = 1024; % Number of samples to generate


f = 1; % Frequency of the sine wave
FS = 200; % Sampling frequency
n = 0:N-1; % Sampling index

% Generate x(n)
x = sin(2 * pi * f * n / FS);

% Generate y(n) with noise


noise = 0.5 * randn(1, N); % Adding Gaussian noise
y = x + noise;

% Plot x(n)
subplot(3, 1, 1);

27
ET 20006: Lab Digital Signal Processing
plot(x);
title('Pure Sine-wave');
grid on;

% Plot y(n), Pure Sinewave + Noise


subplot(3, 1, 2);
plot(y);
title('y(n), Pure Sinewave + Noise');
grid on;

% Estimate the cross-correlation


Rxy = xcorr(x, y);

% Plot the cross-correlation Rxy


subplot(3, 1, 3);
plot(Rxy);
title('Cross-correlation Rxy');
grid on;

Python code:
import numpy as np
import matplotlib.pyplot as plt

N = 1024
f=1
FS = 200
n = np.arange(N)

x = np.sin(2 * np.pi * f * n / FS)

noise = 0.5 * np.random.randn(N)


y = x + noise

plt.subplot(3, 1, 1)
plt.plot(x)
plt.title('Pure Sine-wave')
plt.grid(True)

plt.subplot(3, 1, 2)
plt.plot(y)
plt.title('y(n), Pure Sinewave + Noise')
plt.grid(True)
28
ET 20006: Lab Digital Signal Processing
Rxy = np.correlate(x, y, mode='full')

plt.subplot(3, 1, 3)
plt.plot(Rxy)
plt.title('Cross-correlation Rxy')
plt.grid(True)

plt.tight_layout()
plt.show()
The output is:

1200

29
ET 20006: Lab Digital Signal Processing

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy