N = 1000; % Length of signal fs = 200; % Sampling frequency time = (1:N)/fs; % Time vector X = sin(2*pi*10*time) + 0.5*cos(2*pi*37*time); % Our signal
% Frequency information nFrequency = N/2 + 1; % # of frequencies we can extract (including 0) nyquist = fs/2; % Nyquist frequency frequencies = linspace(0,nyquist,nFrequency); % Frequency vector
import numpy as np import matplotlib.pyplot as plt
N = 1000# Length of signal fs = 200# Sampling frequency time = np.arange(1, N+1) / fs # Time vector X = np.sin(2*np.pi*10*time) + 0.5*np.cos(2*np.pi*37*time) # Our signal
# Frequency information nFrequency = N//2 + 1# # of frequencies we can extract (including 0) nyquist = fs/2# Nyquist frequency frequencies = np.linspace(0, nyquist, nFrequency) # Frequency vector
# Fourier transform for i inrange(nFrequency): sine_wave = np.exp(-1j*2*np.pi*frequencies[i]*time) # Construct sine wave fourier[i] = np.dot(sine_wave, X) # Fourier transform as dot product of X and sine wave
# Plot the magnitude of the Fourier coefficients plt.figure() plt.plot(frequencies, np.abs(fourier)) plt.show()