0% found this document useful (0 votes)
16 views

MP Assignment 6

This document contains 4 solutions to a signal processing assignment involving generating signals, taking the discrete Fourier transform (DFT), and plotting the time domain and frequency domain representations. Solution 1 generates 3 sinusoidal signals, superimposes them, and plots the individual signals and resultant signal in the time domain. It then takes the DFT of each signal and plots the frequency spectra. Solution 2 investigates the effect of sampling rate on aliasing. Solutions 3 and 4 generate and transform additional signals like cosines and gaussians, plotting the corresponding time and frequency representations.

Uploaded by

kattarhindu1011
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

MP Assignment 6

This document contains 4 solutions to a signal processing assignment involving generating signals, taking the discrete Fourier transform (DFT), and plotting the time domain and frequency domain representations. Solution 1 generates 3 sinusoidal signals, superimposes them, and plots the individual signals and resultant signal in the time domain. It then takes the DFT of each signal and plots the frequency spectra. Solution 2 investigates the effect of sampling rate on aliasing. Solutions 3 and 4 generate and transform additional signals like cosines and gaussians, plotting the corresponding time and frequency representations.

Uploaded by

kattarhindu1011
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

MP Assignment 6

Solution 1:
import numpy as np
import matplotlib.pyplot as plt
def gen_sig(sr, amp=1,freq = 1):
ts= 1.0/sr
t = np.arange(0,1,ts)
x= amp*np.sin(2*np.pi*freq*t)
return x,t
sr = 100
x,t = gen_sig(100)
freq1, freq2, freq3 = 1, 4, 7
amp1, amp2, amp3 = 3, 1, 0.5
phase = 0
signal1,t = gen_sig(100,3,1)
signal2,t = gen_sig(100,1,4)
signal3,t = gen_sig(100,0.5,7)
resultant_signal = signal1 + signal2 + signal3
plt.figure()
plt.subplot(221)
plt.plot(t, signal1,label= "frequency=1, Amplitude=4")
plt.title('Signal 1: {} Hz, {} Amplitude'.format(freq1, amp1))
plt.ylabel("Amplitude")
plt.legend()
plt.grid()
plt.subplot(2, 2, 2)
plt.plot(t, signal2,label= "frequency=4 , Amplitude=1")
plt.title('Signal 2: {} Hz, {} Amplitude'.format(freq2, amp2))
plt.ylabel("Amplitude")
plt.legend()
plt.grid()
plt.subplot(2, 2, 3)
plt.plot(t, signal3,label= "frequency=4 , Amplitude=1")
plt.title('Signal 3: {} Hz, {} Amplitude'.format(freq3, amp3))
plt.ylabel("Amplitude")
plt.legend()
plt.grid()
plt.subplot(2, 2, 4)
plt.plot(t, resultant_signal, color='red',label="Resuntant wave")
plt.title('Superimposed Signal')
plt.ylabel("Amplitude")
plt.legend()
plt.grid()
plt.tight_layout()
def DFT(h):
N = len(h)
H = np.zeros(N,dtype = "complex_")
for k in range(N):
for i in range(N):
H[k]+= h[i]* np.exp(-2j*np.pi*k*i/N)
return H,N
fre_secp1,N1= DFT(signal1)
fre_secp2,N2= DFT(signal2)
fre_secp3,N3= DFT(signal3)
fre_secp_rest,Nr= DFT(resultant_signal)
plt.figure()

plt.subplot(2,2,1)
plt.stem(np.abs(fre_secp1)/N1,use_line_collection=True,markerfmt=" ")
plt.title("Frequency spectrum of Signal 1")
plt.xlabel("Frequency Bin")
plt.ylabel("Amplitude")
plt.grid()
plt.subplot(2,2,2)
plt.stem(np.abs(fre_secp2)/N2,use_line_collection=True,markerfmt=" ")
plt.title("Frequency spectrum of Signal 2")
plt.xlabel("Frequency Bin")
plt.ylabel("Amplitude")
plt.grid()
plt.subplot(2,2,3)
plt.stem(np.abs(fre_secp3)/N3,use_line_collection=True,markerfmt=" ")
plt.title("Frequency spectrum of Signal 3")
plt.xlabel("Frequency Bin")
plt.ylabel("Amplitude")
plt.grid()
plt.subplot(2,2,4)
plt.stem(np.abs(fre_secp_rest)/Nr,use_line_collection=True,markerfmt=" ")
plt.title("Frequency spectrum of Resultant Signal")
plt.xlabel("Frequency Bin")
plt.ylabel("Amplitude")
plt.grid()
plt.tight_layout()
def IDFT(h):
N = len(h)
H = np.zeros(N,dtype = "complex_")
for k in range(N):
for i in range(N):
H[k]+= h[i]* np.exp(2j*np.pi*k*i/N)
return H,N
reconstructed,NRC = IDFT(resultant_signal)
plt.figure()
plt.subplot(2,1,1)
plt.stem(np.abs(fre_secp_rest)/Nr,use_line_collection=True,markerfmt=" ")
plt.title("Frequency spectrum of Resultant Signal(Frequency Domain)")
plt.xlabel("Frequency Bin")
plt.ylabel("Amplitude")
plt.grid()
plt.subplot(2,1,2)
plt.stem(np.abs(reconstructed)/NRC,use_line_collection=True,basefmt="-b",markerfmt=" ")
plt.title("Frequency spectrum of Resultant Signal(Time Domain)")
plt.xlabel("Frequency Bin")
plt.ylabel("Amplitude")
plt.grid()
plt.tight_layout()

Plots:
Solution 2:
import numpy as np
import matplotlib.pyplot as plt
from scipy.fft import fft, ifft
def gen_sig(sr ,tp):
ts= 1.0/sr
t = np.arange(0,tp,ts)
x= 8 * np.sin(6 * np.pi * t) * np.cos(2 * np.pi * t) + 3 * np.sin(8 * np.pi * t) + 6 * np.sin(4 * np.pi
* t)
return x,t
sampling_rates = [2**i for i in range(0,6,1)]
print(sampling_rates)
plt.figure(figsize=(12,15))
for i in range(0,6):
signal,t = gen_sig(sampling_rates[i],2)
plt.subplot(6, 2, 1*i +1)
plt.plot(t, signal)
plt.title(f'Time Domain Signal (Sampling Rate = {sampling_rates[i]} Hz)')
plt.xlabel('Time (s)')
plt.ylabel('f(t)')
plt.grid()
plt.tight_layout()
plt.figure(figsize=(12,15))
for i in range(0,6):
signal,t = gen_sig(sampling_rates[i],2)
frequency_spectrum= fft(signal)
plt.subplot(6, 2, 1*i +1)
plt.stem(np.abs(frequency_spectrum),markerfmt=" ")
plt.title('Frequency Spectrum')
plt.xlabel('Frequency Bin')
plt.ylabel('Amplitude')
plt.grid()
plt.tight_layout()
print("The Nyquist-Shannon sampling theorem states that the minimum sampling \n rate
should be at least twice the maximum frequency present in the \n signal to avoid aliasing. In
this case, the signal has components \nwith frequencies of 6π, 2π, 8π, and 4π. Therefore, the
minimum ,\n sampling rate should be greater than 2 * 8π (which is the maximum\n frequency")
plt.figure(figsize=(12,15))
for i in range(0,6):
signal,t = gen_sig(sampling_rates[i],2)
frequency_spectrum= fft(signal)
frequency_spectrum_inv = ifft(frequency_spectrum)
plt.subplot(6, 2, 1*i +1)
plt.stem(np.abs(frequency_spectrum_inv),markerfmt=" ")
plt.title('Reconstructed ime Domain SIgnal')
plt.xlabel('Frequency Bin')
plt.ylabel('Reconstructed f(t)')
plt.grid()
plt.tight_layout()
plt.show()

Console Output:

PLOT:
Solution 3:

import numpy as np

import matplotlib.pyplot as plt

def FFT(x):

N = len(x)

if N == 1:

return x

else:

X_even = FFT(x[::2])

X_odd = FFT(x[1::2])

factor = np.exp(-2j * np.pi * np.arange(N) / N)

X = np.concatenate([X_even + factor[:int(N/2)] * X_odd, X_even + factor[int(N/2):] * X_odd])

return X

def gen_sig1(sr):

ts = 1.0 / sr

t = np.arange(0, 1, ts)

x = np.cos(10*np.pi*t)

return x, t

def gen_sig2(sr):

ts = 1.0 / sr

t = np.arange(0, 1, ts)

x = np.exp(-4*t)*np.cos(10*np.pi*t)

return x, t

t1,t= gen_sig1(64)

t2,t= gen_sig2(64)

plt.subplot(2,1,1)

plt.title("$Time domain graph of f(t) = cos(10 \pi t)$")


plt.plot(t,t1)

plt.xlabel("TIme")

plt.ylabel("Amplitube")

plt.grid()

plt.subplot(2,1,2)

plt.title("$Time domain graph of f(t) = e^{-4t} cos(10\pi t)$")

plt.plot(t,t2)

plt.xlabel("TIme")

plt.ylabel("Amplitube")

plt.grid()

plt.tight_layout()

plt.figure()

ft1= FFT(t1)

ft2= FFT(t2)

plt.subplot(211)

plt.stem(np.abs(ft1),markerfmt=" ")

plt.title('$Frequency Spectrum of f(t) = cos(10 \pi t)$')

plt.xlabel('Frequency Bin')

plt.ylabel('Amplitude')

plt.grid()

plt.subplot(212)

plt.stem(np.abs(ft1),markerfmt=" ")

plt.title('$Frequency Spectrum f(t) = e^{-4t} cos(10\pi t)$')

plt.xlabel('Frequency Bin')

plt.ylabel('Amplitude')

plt.grid()

plt.tight_layout()
print("The spectrum should show peaks at frequencies corresponding to 10π and 10π - 4.The
magnitude of these peaks represents the amplitudes of the cosine waves.")

Console Output:

The spectrum should show peaks at frequencies corresponding to 10π and 10π - 4.The
magnitude of these peaks represents the amplitudes of the cosine waves.

PLOT:

Solution 4:

import numpy as np

import matplotlib.pyplot as plt

from scipy.fft import fft

def gaussian1(sr ,si,ti,tp):

ts= 1.0/sr

t = np.arange(ti,tp+ts,ts)

x=((1/(np.sqrt(2*np.pi*si**2))*np.exp(-(t)**2)/(2*si**2)))

return x,t

def gaussian2(sr ,si,ti,tp):

ts= 1.0/sr

t = np.arange(ti,tp+ts,ts)

x=((1/(np.sqrt(2*np.pi*si**2))*np.exp(-(t-5)**2)/(2*si**2)))

return x,t
def sine(sr ,ti,tp):

ts= 1.0/sr

t = np.arange(ti,tp+ts,ts)

x= np.sin(10*np.pi*t + (np.pi/6))

return x,t

def rss(sr,ti,tp):

ts= 1.0/sr

t = np.arange(ti,tp+ts,ts)

x= np.abs(np.sin(10*np.pi*t + (np.pi/6)))

return x,t

def rect(sr,ti,tp):

ts= 1.0/sr

t = np.arange(ti,tp+ts,ts)

x = np.zeros(len(t))

x[(t >= 0) & (t <= 1)] = 5

return x,t

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

plt.subplot(321)

s1, t = gaussian1(10, 0.02, -2, 2)

plt.plot(t, s1)

plt.ylabel("Amplitude")

plt.xlabel("Time")

plt.title("Time domain graph of $(1/(\sqrt{2\pi}\sigma(0.02)))e^{-t^2/(2\sigma^2)}$")

plt.grid()

plt.subplot(323)

s2, t = gaussian1(10, 0.04, -2, 2)

plt.plot(t, s2)
plt.ylabel("Amplitude")

plt.xlabel("Time")

plt.title("Time domain graph of $(1/(\sqrt{2\pi}\sigma(0.04)))e^{-t^2/(2\sigma^2)}$")

plt.grid()

plt.subplot(325)

s3, t = gaussian1(10, 0.06, -2, 2)

plt.plot(t, s3)

plt.ylabel("Amplitude")

plt.xlabel("Time")

plt.title("Time domain graph of $(1/(\sqrt{2\pi}\sigma(0.06)))e^{-t^2/(2\sigma^2)}$")

plt.grid()

plt.subplot(322)

f1= fft(s1)

plt.stem((np.abs(f1))**2/(len(f1)**2),markerfmt=" ")

plt.title('Power Spectrum of $(1/(\sqrt{2\pi}\sigma(0.02)))e^{-t^2/(2\sigma^2)}$')

plt.xlabel('Frequency Bin')

plt.ylabel('Amplitude')

plt.grid()

plt.subplot(324)

f2= fft(s2)

plt.stem((np.abs(f2))**2/(len(f2)**2),markerfmt=" ")

plt.title('Power Spectrum of $(1/(\sqrt{2\pi}\sigma(0.04)))e^{-t^2/(2\sigma^2)}$')

plt.xlabel('Frequency Bin')

plt.ylabel('Amplitude')

plt.grid()

plt.subplot(326)

f3= fft(s3)
plt.stem((np.abs(f3))**2/(len(f3)**2),markerfmt=" ")

plt.title('Power Spectrum of $(1/(\sqrt{2\pi}\sigma(0.06)))e^{-t^2/(2\sigma^2)}$')

plt.xlabel('Frequency Bin')

plt.ylabel('Amplitude')

plt.grid()

plt.tight_layout()

plt.figure()

plt.subplot(211)

s21, t = gaussian2(10, 0.05, 0, 10)

plt.plot(t, s21)

plt.ylabel("Amplitude")

plt.xlabel("Time")

plt.title("Time domain graph of $(1/(\sqrt{2\pi}\sigma))e^{-(t-5)^2/(2\sigma^2)}$")

plt.grid()

plt.subplot(212)

f21= fft(s21)

plt.stem((np.abs(f21))**2/(len(f21)**2),markerfmt=" ")

plt.title('Power Spectrum of $(1/(\sqrt{2\pi}\sigma))e^{-(t-5)^2/(2\sigma^2)}$')

plt.xlabel('Frequency Bin')

plt.ylabel('Amplitude')

plt.grid()

plt.tight_layout()

plt.figure()

plt.subplot(211)

s31, t = sine(1000, 0, 1)

plt.plot(t, s31)

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

plt.title("Time domain graph of \\sin(10\pi t + \pi/6)$")

plt.grid()

plt.subplot(212)

f31= fft(s31)

plt.stem((np.abs(f31))**2/(len(f31)**2),markerfmt=" ")

plt.title('Power Spectrum of $\\sin(10\pi t + \pi/6)$')

plt.xlabel('Frequency Bin')

plt.ylabel('Amplitude')

plt.grid()

plt.tight_layout()

plt.figure()

plt.subplot(211)

s41, t = rss(100, 0, 1)

plt.plot(t, s41)

plt.ylabel("Amplitude")

plt.xlabel("Time")

plt.title("Time domain graph of |\\sin(10\pi t + \pi/6)|$")

plt.grid()

plt.subplot(212)

f41= fft(s41)

plt.stem((np.abs(f41))**2/(len(f41)**2),markerfmt=" ")

plt.title('Power Spectrum of $|\\sin(10\pi t + \pi/6)|$')

plt.xlabel('Frequency Bin')

plt.ylabel('Amplitude')

plt.grid()

plt.tight_layout()
plt.figure()

plt.subplot(211)

s51, t = rect(1000, 0, 1)

plt.plot(t, s51)

plt.ylabel("Amplitude")

plt.xlabel("Time")

plt.title("Time domain graph of 5")

plt.grid()

plt.subplot(212)

f51= fft(s51)

plt.stem((np.abs(f51))**2/(len(f51)**2),markerfmt=" ")

plt.title('Power Spectrum of 5')

plt.xlabel('Frequency Bin')

plt.ylabel('Amplitude')

plt.grid()

plt.tight_layout()

PLOT:

You might also like