DSP LAB Manual
DSP LAB Manual
MIS – 112107011
Batch – A
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 –
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 –
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 –
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.show()
Output –
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.show()
Output –
2) Cosine Wave
i) Frequency = 1Hz
Code –
import numpy as np
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.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.show()
Output –
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.show()
Output –
3) Discrete Cosine Wave
Code –
import numpy as np
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.show()
Output –
Code –
import numpy as np
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.show()
Output –
Code –
import numpy as np
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.show()
Output –
Code –
import numpy as np
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.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
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.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)
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)
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)
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)
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)
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
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:
[Biased Autocorrelation]
(2)
[Uniased Autocorrelation]
= 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:
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)
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()
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
% Generate x(n)
x = sin(2 * pi * f * n / FS);
% Plot x(n)
subplot(3, 1, 1);
27
ET 20006: Lab Digital Signal Processing
plot(x);
title('Pure Sine-wave');
grid on;
Python code:
import numpy as np
import matplotlib.pyplot as plt
N = 1024
f=1
FS = 200
n = np.arange(N)
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