📖 Scenario: You have recorded a simple sound signal and want to understand its frequency components. This is like listening to a song and figuring out which musical notes are playing.
🎯 Goal: Build a small program that creates a signal, sets a frequency threshold, finds frequencies above that threshold, and prints them out.
📋 What You'll Learn
Create a dictionary with frequency values and their amplitudes
Set a threshold amplitude value
Use a dictionary comprehension to select frequencies with amplitude above the threshold
Print the filtered frequencies and their amplitudes
💡 Why This Matters
🌍 Real World
Frequency spectrum analysis helps in music, engineering, and science to understand signals and sounds.
💼 Career
Data scientists and engineers use frequency analysis to process audio, vibrations, and other signals in many industries.
Progress0 / 4 steps
1
Create the frequency spectrum dictionary
Create a dictionary called frequency_spectrum with these exact entries: 100: 0.5, 200: 1.2, 300: 0.9, 400: 0.3, 500: 1.5
Signal Processing
Hint
Use curly braces {} to create a dictionary with frequency keys and amplitude values.
2
Set the amplitude threshold
Create a variable called threshold and set it to 1.0
Signal Processing
Hint
Just assign the number 1.0 to the variable threshold.
3
Filter frequencies above the threshold
Use a dictionary comprehension to create a new dictionary called filtered_frequencies that contains only the frequencies and amplitudes from frequency_spectrum where the amplitude is greater than threshold
Signal Processing
Hint
Use {freq: amp for freq, amp in frequency_spectrum.items() if amp > threshold} to filter the dictionary.
4
Print the filtered frequencies
Write a print statement to display the filtered_frequencies dictionary
Signal Processing
Hint
Use print(filtered_frequencies) to show the result.
Practice
(1/5)
1. What does a peak in a frequency spectrum represent in signal processing?
easy
A. A strong presence of a specific frequency in the signal
B. The time duration of the signal
C. The amplitude of the original time signal
D. The noise level in the signal
Solution
Step 1: Understand frequency spectrum meaning
The frequency spectrum shows how much of each frequency is present in the signal.
Step 2: Interpret peaks in the spectrum
A peak means that frequency has a strong presence or high amplitude in the signal.
Final Answer:
A strong presence of a specific frequency in the signal -> Option A
Quick Check:
Peak = strong frequency presence [OK]
Hint: Peaks show strong frequencies in the signal [OK]
Common Mistakes:
Confusing time duration with frequency
Thinking amplitude in time domain equals peak in spectrum
Assuming noise causes peaks
2. Which of the following is the correct way to compute the frequency spectrum of a signal in Python using numpy?
easy
A. np.fft.freq(signal)
B. np.fft.ifft(signal)
C. np.fft.fft(signal)
D. np.fft.spectrum(signal)
Solution
Step 1: Recall numpy FFT functions
np.fft.fft() computes the Fourier Transform to get frequency spectrum.
Step 2: Check other options
np.fft.ifft() computes inverse FFT, others do not exist.
Final Answer:
np.fft.fft(signal) -> Option C
Quick Check:
FFT = np.fft.fft() [OK]
Hint: Use np.fft.fft() for frequency spectrum [OK]
B. Using np.argmax on complex spectrum instead of magnitude
C. Signal frequency is too high for sampling rate
D. Not normalizing the spectrum before argmax
Solution
Step 1: Check how peak is found
np.argmax(spectrum) finds max of complex numbers, which is incorrect.
Step 2: Correct method to find peak
Use np.argmax(np.abs(spectrum)) to find peak magnitude index.
Final Answer:
Using np.argmax on complex spectrum instead of magnitude -> Option B
Quick Check:
Peak must be on magnitude, not complex values [OK]
Hint: Use magnitude (abs) before argmax [OK]
Common Mistakes:
Argmax on complex array directly
Confusing FFT and IFFT
Ignoring magnitude for peak detection
5. You have a signal composed of two sine waves at 3 Hz and 7 Hz sampled at 50 Hz for 2 seconds. Which code snippet correctly identifies both frequencies from the frequency spectrum?
hard
A. import numpy as np
t = np.linspace(0, 2, 100)
signal = np.sin(2*np.pi*3*t) + np.sin(2*np.pi*7*t)
spectrum = np.fft.fft(signal)
freqs = np.fft.fftfreq(len(t), d=1/50)
peaks = freqs[np.argsort(np.abs(spectrum))[-2:]]
print(np.sort(np.abs(peaks)))
B. import numpy as np
t = np.linspace(0, 2, 100)
signal = np.sin(2*np.pi*3*t) + np.sin(2*np.pi*7*t)
spectrum = np.fft.ifft(signal)
freqs = np.fft.fftfreq(len(t), d=1/50)
peaks = freqs[np.argmax(np.abs(spectrum))]
print(peaks)
C. import numpy as np
t = np.linspace(0, 2, 50)
signal = np.sin(2*np.pi*3*t) + np.sin(2*np.pi*7*t)
spectrum = np.fft.fft(signal)
freqs = np.fft.fftfreq(len(t), d=1/50)
peaks = freqs[np.argsort(np.abs(spectrum))[-2:]]
print(np.sort(np.abs(peaks)))
D. import numpy as np
t = np.linspace(0, 2, 100)
signal = np.sin(2*np.pi*3*t) + np.sin(2*np.pi*7*t)
spectrum = np.fft.fft(signal)
freqs = np.fft.fftfreq(len(t), d=1/100)
peaks = freqs[np.argsort(np.abs(spectrum))[-2:]]
print(np.sort(np.abs(peaks)))
Solution
Step 1: Check sampling and signal length
Sampling at 50 Hz for 2 seconds means 100 samples, so t has 100 points.
Step 2: Verify FFT and frequency calculation
Use np.fft.fft(signal) and np.fft.fftfreq with d=1/50 for correct frequency bins.
Step 3: Identify two largest peaks
Sort magnitudes and pick last two indices for peaks, then print sorted absolute frequencies.
Final Answer:
Code snippet D correctly identifies both frequencies -> Option A
Quick Check:
Correct sampling, FFT, freq bins, and peak selection [OK]
Hint: Match sample count and rate, use fftfreq with correct d [OK]
Common Mistakes:
Using inverse FFT instead of FFT
Wrong sample count or sampling interval
Incorrect frequency bin calculation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.