📖 Scenario: Imagine you recorded a sound that is a mix of two musical notes played together. You want to find out which notes (frequencies) are in the sound.
🎯 Goal: You will create a simple signal with two frequencies, use Fourier transform to find these frequencies, and then display the result.
📋 What You'll Learn
Create a time array for the signal
Create a signal with two sine waves of different frequencies
Use Fourier transform to find frequency components
Print the frequencies found in the signal
💡 Why This Matters
🌍 Real World
Fourier transforms help us find hidden frequencies in signals like music, sound, or sensor data.
💼 Career
Data scientists use Fourier analysis in fields like audio processing, communications, and vibration analysis.
Progress0 / 4 steps
1
Create a time array for the signal
Create a variable called t that is a numpy array of 500 points evenly spaced from 0 to 1 second.
SciPy
Hint
Use np.linspace(0, 1, 500) to create the time array.
2
Create a signal with two sine waves
Create a variable called signal that is the sum of two sine waves: one with frequency 5 Hz and one with frequency 20 Hz, both using the time array t. Use np.sin() and multiply the frequency by 2 * np.pi * t.
SciPy
Hint
Use np.sin(2 * np.pi * frequency * t) for each sine wave and add them.
3
Apply Fourier transform to find frequencies
Import fft and fftfreq from scipy.fft. Create a variable called fft_values by applying fft to signal. Create a variable called frequencies by applying fftfreq to the length of signal and the sample spacing 1/500.
SciPy
Hint
Use fft(signal) and fftfreq(len(signal), 1/500) to get frequency components.
4
Print the main frequencies found
Print the frequencies where the absolute value of fft_values is greater than 100. Use a for loop with variables freq and value iterating over zip(frequencies, fft_values). Print freq rounded to 1 decimal place.
SciPy
Hint
Check absolute fft values and print frequencies where value is large.
Practice
(1/5)
1. Why does a Fourier transform help us understand a signal better?
easy
A. Because it averages the signal values over time
B. Because it removes noise from the signal
C. Because it changes the signal into a longer time series
D. Because it shows the different frequencies inside the signal
Solution
Step 1: Understand what a Fourier transform does
A Fourier transform breaks a signal into its frequency parts, showing which frequencies are present.
Step 2: Compare options to this understanding
Only Because it shows the different frequencies inside the signal correctly states that it reveals the different frequencies inside the signal.
Final Answer:
Because it shows the different frequencies inside the signal -> Option D
Quick Check:
Fourier transform = frequency breakdown [OK]
Hint: Fourier transform = signal to frequency parts [OK]
Common Mistakes:
Thinking Fourier transform removes noise
Confusing time and frequency domains
Believing it averages signal values
2. Which of the following is the correct way to import the Fourier transform function from scipy?
easy
A. import scipy.fftpack.fft
B. from scipy.fft import fft
C. from scipy import fourier_transform
D. import fft from scipy
Solution
Step 1: Recall the correct scipy module for Fourier transforms
The modern scipy module for Fourier transforms is scipy.fft, and the function is fft.
Step 2: Check each import statement
Only 'from scipy.fft import fft' is valid Python syntax and correct module usage.
Final Answer:
from scipy.fft import fft -> Option B
Quick Check:
Correct import = from scipy.fft import fft [OK]
Hint: Use scipy.fft for Fourier transforms in recent versions [OK]
Common Mistakes:
Using scipy.fftpack which is older
Wrong import syntax causing errors
Assuming function name is fourier_transform
3. What is the output of this code snippet?
import numpy as np
from scipy.fft import fft
signal = np.array([1, 0, -1, 0])
result = fft(signal)
print(np.round(result, 2))
medium
A. [0.+0.j 2.+0.j 0.+0.j 2.+0.j]
B. [0.+0.j 2.+0.j 0.-0.j 2.-0.j]
C. [0.+0.j 1.+0.j 0.+0.j 1.+0.j]
D. [0.+0.j 0.+0.j 0.+0.j 0.+0.j]
Solution
Step 1: Understand the input signal and FFT output
The signal is [1, 0, -1, 0], a simple wave. FFT returns complex numbers representing frequency components.
Step 2: Calculate FFT and round results
FFT of this signal is [0.+0.j 2.+0.j 0.+0.j 2.+0.j]. Rounding to 2 decimals keeps these values.