Fourier Analysis
Fourier Analysis allows us to decompose complex, periodic signals into a sum of simple sine and cosine waves.
Fourier Series
Any periodic function with period can be represented as:
python
1import numpy as np
2
3def square_wave_fourier(x, terms):
4 res = 0
5 for n in range(1, terms + 1, 2):
6 # f(x) = (4/pi) * summation sin(n*x)/n
7 res += (4 / np.pi) * (np.sin(n * x) / n)
8 return res
9
10x_val = np.pi/2
11print(f"Square wave approx (5 terms) at pi/2: {square_wave_fourier(x_val, 5)}")
12print(f"Square wave approx (100 terms) at pi/2: {square_wave_fourier(x_val, 100)}")
The Fourier Transform
For non-periodic functions, the Fourier Transform maps a function from the time (or space) domain to the frequency domain:
python
1import numpy as np
2
3# Fast Fourier Transform (FFT)
4signal = np.array([1, 0, 1, 0, 1, 0, 1, 0])
5fft_res = np.fft.fft(signal)
6
7print(f"Signal: {signal}")
8print(f"FFT Magnitudes: {np.abs(fft_res).round(2)}")
9print("The spike at index 4 represents the fundamental frequency of the oscillation.")
Parseval’s Theorem
Parseval’s Theorem states that the total energy of a signal in the time domain is equal to the total energy in the frequency domain: