Finished re-writing lab 1. Getting started guide introduces variables, functions, and logic
This commit is contained in:
@@ -1,2 +1,2 @@
|
||||
syms t
|
||||
a=simplify(fourier(sin(0.5*t)/(0.5*t)))
|
||||
syms t % Define t as a symbolic variable
|
||||
a=simplify(fourier(sin(0.5*t)/(0.5*t))) % Print the simplified Fourier transform of sin(0.5t)/(0.5t) = sinc(0.5t)
|
||||
|
||||
@@ -3,45 +3,18 @@ ECE 09351 - Digital Signal Processing
|
||||
Lab 1 Question 3
|
||||
|
||||
Translated from MATLAB by Aidan Sharpe
|
||||
Last Modified: January 17, 2025
|
||||
Last Modified: January 18, 2025
|
||||
"""
|
||||
|
||||
import numpy as np
|
||||
import scipy as sp
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
|
||||
def cft(g, f):
|
||||
result = np.zeros(len(f), dtype=complex)
|
||||
|
||||
for i, ff in enumerate(f):
|
||||
result[i] = complex_quad(lambda t: g(t)*np.exp(-2j*np.pi*ff*t), -5, 5)
|
||||
|
||||
return result
|
||||
|
||||
def complex_quad(g, a, b):
|
||||
t = np.linspace(a, b, 2501)
|
||||
x = g(t)
|
||||
return sp.integrate.simpson(y=x, x=t)
|
||||
from sympy import simplify, fourier_transform, sinc
|
||||
from sympy.abc import t, f
|
||||
|
||||
|
||||
def main():
|
||||
g = lambda t: np.sinc(0.5*t/np.pi)
|
||||
|
||||
f_s = 1
|
||||
T_s = 1/f_s
|
||||
t_0 = 1000
|
||||
t = np.arange(-t_0, t_0, T_s)
|
||||
f = np.linspace(-f_s/2, f_s/2, len(t), endpoint=False)
|
||||
omega = 2*np.pi*f
|
||||
|
||||
G = np.fft.fftshift(np.fft.fft(g(t)) * np.exp(-2j*np.pi*f*t_0) /f_s)
|
||||
G_analytic = 2*np.pi*(np.heaviside(omega+0.5, 1) - np.heaviside(omega-0.5, 1))
|
||||
|
||||
plt.plot(f, G_analytic, color="red")
|
||||
plt.scatter(f, G)
|
||||
plt.show()
|
||||
|
||||
x = sinc(0.5*t) # Define x as sinc(0.5t) = sin(0.5t)/(0.5t)
|
||||
X = simplify(fourier_transform(x, t, f)) # Evaluate the Fourier transform of x, converting from t domain to f domain
|
||||
print(X)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
% Lab 1
|
||||
% Question 4
|
||||
|
||||
fs=4;
|
||||
ts=1/fs;
|
||||
fs=4; % Sample frequency 4[Hz]
|
||||
ts=1/fs; % Period is reciprocal of frequency
|
||||
|
||||
n=0:1:99;
|
||||
n=0:1:99; % Sample indicies (0 to 99)
|
||||
x(n+1)=cos(2*pi*n*ts/5)+sin(4*pi*n*ts/7)+cos(16*pi*n*ts/9);
|
||||
|
||||
stem(n,x,'linewidth',2);
|
||||
stem(n,x,'linewidth',2); % Plot samples on stem plot
|
||||
xlabel('n')
|
||||
ylabel('x(n)')
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
"""
|
||||
ECE 09351 - Digital Signal Processing
|
||||
Lab 1 Question 4
|
||||
|
||||
Translated from MATLAB by Aidan Sharpe
|
||||
Last Modified: January 18, 2025
|
||||
"""
|
||||
|
||||
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
|
||||
def main():
|
||||
f_s = 4 # Sample frequency 4[Hz]
|
||||
T_s = 1/f_s # Period is reciprocal of frequency
|
||||
|
||||
n = np.arange(100) # Sample indicies (0 to 99)
|
||||
t = n*T_s # Conversion between index and time
|
||||
|
||||
x = np.cos(2*np.pi*t/5) + np.sin(4*np.pi*t/7) + np.cos(16*np.pi*t/9)
|
||||
|
||||
plt.stem(n, x) # Plot samples against index on stem plot
|
||||
plt.xlabel("n")
|
||||
plt.ylabel("x(n)")
|
||||
plt.show() # Show the plot
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,3 +1,3 @@
|
||||
syms t
|
||||
f=exp(-t*4)*heaviside(t);
|
||||
ff=fourier(f);
|
||||
syms t % Define t as a symbolic variable
|
||||
f=exp(-t*4)*heaviside(t); % Define f(t) = e^(-4t)*u(t)
|
||||
ff=fourier(f); % Take the Fourier transform of f(t)
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
"""
|
||||
ECE 09351 - Digital Signal Processing
|
||||
Lab 1 Question 5
|
||||
|
||||
Translated from MATLAB by Aidan Sharpe
|
||||
Last Modified: January 18, 2025
|
||||
"""
|
||||
|
||||
from sympy import fourier_transform, exp, Q
|
||||
from sympy.abc import t, f
|
||||
|
||||
def main():
|
||||
x = exp(-4*t)*Heaviside(t) # Define x(t) = e^(-4t)*u(t)
|
||||
X = fourier_transform(x, t, f) # Take the Fourier transform of x(t)
|
||||
print(x)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,9 +1,8 @@
|
||||
syms a b t
|
||||
assume(a > 0)
|
||||
f = exp(-a*abs(t));
|
||||
ff = fourier(f);
|
||||
|
||||
assume(b > 0)
|
||||
g = f*cos(b*t);
|
||||
gg = simplify(fourier(g));
|
||||
syms a b t % Define a, b, and t as symbolic variables
|
||||
assume(a > 0) % Assume a is a positive real number
|
||||
f = exp(-a*abs(t)); % Define f(t) = e^(-a|t|)
|
||||
ff = fourier(f) % Take the Fourier transform of f(t)
|
||||
|
||||
assume(b > 0) % Assume b is a positive real number
|
||||
g = f*cos(b*t); % Define g(t) = e^(-a|t|)*cos(bt)
|
||||
gg = simplify(fourier(g)) % Take the Fourier transform of g(t)
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
"""
|
||||
ECE 09351 - Digital Signal Processing
|
||||
Lab 1 Question 6
|
||||
|
||||
Translated from MATLAB by Aidan Sharpe
|
||||
Last Modified: January 18, 2025
|
||||
"""
|
||||
|
||||
from sympy import *
|
||||
from sympy.abc import a, b, t, w
|
||||
|
||||
|
||||
# Please note that the answers are given as piecewise functions.
|
||||
# The conditions are unnecessary, so take the first part of the piecewise result.
|
||||
def main():
|
||||
# Assume that a and b are positive real numbers
|
||||
with assuming(Q.positive(a), Q.positive(b)):
|
||||
x_1 = exp(-a*abs(t)) # x1(t) = e^(-a|t|)
|
||||
X_1 = integrate(x_1*exp(-1j*w*t), (t, -oo, oo)) # Fourier transform of x1(t)
|
||||
print(simplify(X_1))
|
||||
|
||||
x_2 = x_1*cos(b*t) # x2(t) = x1(t)*cos(bt)
|
||||
X_2 = integrate(x_2*exp(-1j*w*t), (t, -oo, oo)) # Fourier transform of x2(t)
|
||||
print(simplify(X_2))
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user