Introduction
Frequency spectrum interpretation helps us understand what frequencies make up a signal. It shows the strength of each frequency, like seeing the ingredients in a recipe.
1. Obtain the signal data. 2. Apply a Fourier Transform (like FFT) to convert the signal from time to frequency. 3. Plot or analyze the magnitude of the frequency components. 4. Interpret peaks as dominant frequencies.
import numpy as np import matplotlib.pyplot as plt # Create a signal with two frequencies fs = 1000 # Sampling rate T = 1.0 # seconds t = np.linspace(0, T, int(fs*T), endpoint=False) signal = 0.5*np.sin(2*np.pi*50*t) + 0.3*np.sin(2*np.pi*120*t) # Compute FFT fft_vals = np.fft.fft(signal) freqs = np.fft.fftfreq(len(signal), 1/fs) # Plot magnitude spectrum plt.plot(freqs[:len(freqs)//2], np.abs(fft_vals)[:len(fft_vals)//2]) plt.xlabel('Frequency (Hz)') plt.ylabel('Magnitude') plt.title('Frequency Spectrum') plt.show()
# After FFT, find the frequency with the highest magnitude peak_freq = freqs[np.argmax(np.abs(fft_vals))] print(f"Dominant frequency: {abs(peak_freq):.1f} Hz")
import numpy as np import matplotlib.pyplot as plt # Sampling parameters fs = 500 # Sampling frequency in Hz T = 2.0 # Duration in seconds # Time array t = np.linspace(0, T, int(fs*T), endpoint=False) # Signal: sum of 60 Hz and 150 Hz sine waves signal = 1.0*np.sin(2*np.pi*60*t) + 0.5*np.sin(2*np.pi*150*t) # Compute FFT fft_vals = np.fft.fft(signal) freqs = np.fft.fftfreq(len(signal), 1/fs) # Only positive frequencies pos_mask = freqs >= 0 freqs = freqs[pos_mask] magnitudes = np.abs(fft_vals[pos_mask]) # Print dominant frequencies (top 2 peaks) top_indices = magnitudes.argsort()[-2:][::-1] for i in top_indices: print(f"Frequency: {freqs[i]:.1f} Hz, Magnitude: {magnitudes[i]:.2f}") # Plot frequency spectrum plt.plot(freqs, magnitudes) plt.xlabel('Frequency (Hz)') plt.ylabel('Magnitude') plt.title('Frequency Spectrum Interpretation') plt.grid(True) plt.show()
import numpy as np signal = np.sin(2 * np.pi * 5 * np.linspace(0, 1, 100)) spectrum = np.fft.fft(signal) peak_freq_index = np.argmax(np.abs(spectrum)) print(peak_freq_index)
import numpy as np signal = np.sin(2 * np.pi * 10 * np.linspace(0, 1, 100)) spectrum = np.fft.fft(signal) peak = np.argmax(spectrum) print(peak)
Pass the test and this pattern moves unknown