Complete the code to compute the frequency components of a signal using FFT.
import numpy as np signal = np.array([1, 2, 3, 4, 5]) freq_components = np.fft.[1](signal)
The fft function computes the Fast Fourier Transform, which converts a signal from time domain to frequency domain.
Complete the code to calculate the magnitude spectrum from the FFT result.
import numpy as np signal = np.array([1, 2, 3, 4, 5]) freq_components = np.fft.fft(signal) magnitude = np.abs([1])
The magnitude spectrum is the absolute value of the FFT output, which is stored in freq_components.
Fix the error in the code to correctly compute the frequency bins for the FFT of a signal sampled at 1000 Hz.
import numpy as np sampling_rate = 1000 signal = np.array([0, 1, 0, -1] * 10) n = len(signal) freq_bins = np.fft.fftfreq(n, d=[1])
The d parameter in fftfreq is the sample spacing, which is the inverse of the sampling rate.
Fill both blanks to create a dictionary of frequencies and their magnitudes for frequencies above 10 Hz.
import numpy as np signal = np.array([1, 2, 3, 4, 5, 6, 7, 8]) sampling_rate = 100 n = len(signal) freqs = np.fft.fftfreq(n, d=1/sampling_rate) fft_vals = np.fft.fft(signal) magnitude = np.abs(fft_vals) freq_mag_dict = {freqs[i]: magnitude[i] for i in range(n) if freqs[i] [1] [2]
The dictionary comprehension filters frequencies greater than 10 Hz using freqs[i] > 10.
Fill all three blanks to create a dictionary of frequencies and magnitudes for positive frequencies only.
import numpy as np signal = np.array([1, 2, 3, 4, 5]) sampling_rate = 50 n = len(signal) freqs = np.fft.fftfreq(n, d=1/sampling_rate) fft_vals = np.fft.fft(signal) magnitude = np.abs(fft_vals) positive_freqs = { [1]: [2] for i in range(n) if freqs[i] [3] 0 }
The dictionary comprehension maps each positive frequency freqs[i] to its magnitude magnitude[i] where frequency is greater than zero.
