The Fast Fourier Transform (FFT) helps us quickly find the different frequencies inside a signal. It changes a signal from time view to frequency view so we can understand its parts better.
Fast Fourier Transform (FFT) algorithm in Signal Processing
FFT_result = FFT(signal) # signal: list or array of numbers representing the signal # FFT_result: complex numbers showing frequency components
The input signal length is often a power of two for fastest results.
The output shows both strength and phase of each frequency.
import numpy as np signal = [1, 0, -1, 0] FFT_result = np.fft.fft(signal) print(FFT_result)
import numpy as np signal = np.sin(2 * np.pi * 5 * np.linspace(0, 1, 100, endpoint=False)) FFT_result = np.fft.fft(signal) print(abs(FFT_result))
This program creates a signal with two known frequencies, finds its frequency parts using FFT, prints the main frequencies detected, and shows a graph of frequency strengths.
import numpy as np import matplotlib.pyplot as plt # Create a signal with two frequencies: 5 Hz and 20 Hz sampling_rate = 100 # samples per second T = 1.0 / sampling_rate x = np.linspace(0.0, 1.0, sampling_rate, endpoint=False) signal = 0.7 * np.sin(2.0 * np.pi * 5.0 * x) + 0.3 * np.sin(2.0 * np.pi * 20.0 * x) # Compute FFT FFT_result = np.fft.fft(signal) # Compute frequencies for x-axis freqs = np.fft.fftfreq(len(signal), T) # Take only positive frequencies and their magnitudes positive_freqs = freqs[:len(freqs)//2] magnitudes = np.abs(FFT_result)[:len(freqs)//2] # Print main frequencies found main_freqs = positive_freqs[magnitudes > 10] print('Main frequencies detected (Hz):', main_freqs) # Plot the magnitude spectrum plt.plot(positive_freqs, magnitudes) plt.title('Frequency components of the signal') plt.xlabel('Frequency (Hz)') plt.ylabel('Magnitude') plt.grid(True) plt.show()
FFT output contains complex numbers; use abs() to get strength.
FFT works best when signal length is a power of two, but libraries handle other lengths too.
Frequency bins correspond to specific frequencies based on sampling rate and signal length.
FFT quickly finds frequency parts inside a signal.
It helps analyze sounds, vibrations, and other signals in frequency view.
Use libraries like NumPy to easily apply FFT and understand signal frequencies.
Practice
Fast Fourier Transform (FFT) algorithm in signal processing?Solution
Step 1: Understand FFT role and compare options
FFT transforms time-domain signal to frequency domain. A matches exactly; B is compression, C filtering, D amplification.Final Answer:
To convert a signal from time domain to frequency domain -> Option BQuick Check:
FFT = Time to Frequency conversion [OK]
- Confusing FFT with noise filtering
- Thinking FFT compresses data
- Assuming FFT amplifies signals
Solution
Step 1: Recall and match Python import syntax
Correct for specific function: 'from numpy.fft import fft' (C). A imports submodule, B aliases submodule (requires fft.fft()), D invalid.Final Answer:
from numpy.fft import fft -> Option DQuick Check:
Correct import syntax = from numpy.fft import fft [OK]
- Using 'import fft from numpy' which is invalid syntax
- Importing whole numpy.fft as fft causing confusion
- Trying 'from numpy import fft' which imports the module, not function
import numpy as np signal = np.array([1, 0, -1, 0]) fft_result = np.fft.fft(signal) print(np.round(fft_result, 2))
What is the output?
Solution
Step 1: Compute and verify FFT output
np.fft.fft([1,0,-1,0]) corresponds to a sine wave component, resulting in imaginary parts at k=1 and k=3. The output is [0.+0.j 0.+2.j 0.+0.j 0.-2.j], matching [0.+0.j 0.+2.j 0.+0.j 0.-2.j].Final Answer:
[0.+0.j 0.+2.j 0.+0.j 0.-2.j] -> Option AQuick Check:
FFT sine wave = imaginary components at k=1,3 [OK]
- Confusing cosine signal with sine (imaginary output)
- Confusing zeros with non-zero frequency components
- Rounding incorrectly or not at all
import numpy as np signal = [1, 2, 3, 4] fft_result = fft(signal) print(fft_result.real)
It raises an error. What is the most likely cause?
Solution
Step 1: Trace code execution for error
fft(signal) raises NameError: 'fft' undefined. Import is numpy as np (use np.fft.fft); no direct fft import (e.g., from numpy.fft import fft). A, B, D unrelated.Final Answer:
Missing import statement for fft -> Option CQuick Check:
Undefined fft = missing import [OK]
- Not importing the fft function when using fft()
- Thinking .real is invalid on FFT output
- Confusing print syntax errors
data. You want to remove frequencies above 100 Hz using FFT. Which approach correctly achieves this?Solution
Step 1: Outline correct FFT low-pass filtering
FFT to freq domain (use fftfreq for bins), zero coeffs where |freq| > 100 (fs=1000, cutoff index ~100/500=0.2*N), ifft back. Matches B precisely; A ignores freq mapping, C/D ineffective.Final Answer:
Compute FFT, zero out components with frequency > 100 Hz, then inverse FFT -> Option AQuick Check:
Zero high freq + ifft = low-pass filter [OK]
- Confusing component count with frequency
- Scaling FFT values instead of zeroing
- Misusing FFT to subtract frequencies directly
Ready to prove it?
Pass the test and this pattern moves unknown
