培训中重制FFT示例遇边界不一致问题,截止频率为2Hz
Hey there! Let's troubleshoot that FFT plot boundary mismatch you're dealing with—reproducing reference examples can be tricky when small details throw things off, but we’ll work through the most likely fixes:
1. Incorrect Frequency Axis Calculation
This is the most frequent culprit. Your 2Hz cutoff needs to align perfectly with the right bin in your FFT frequency axis.
- Double-check how you’re generating the frequency axis. For real-valued signals (which it sounds like you’re working with), use
np.fft.fftfreq(N, 1/Fs)[:N//2+1](replaceNwith your FFT sample count,Fswith your sampling frequency) to get accurate positive frequency bins. - Verify that the 2Hz position matches your example: calculate which index in your frequency array equals 2Hz, and confirm your FFT amplitude is 0 at that exact point.
2. Mismatched Windowing
If you applied a window function (like Hann, Hamming) to your signal before FFT, but the reference example didn’t (or used a different window), this can shift the apparent cutoff boundary even if the overall shape matches.
- Try removing any windowing first to see if the boundary aligns. If the example does use a window, make sure you’re using the exact same window type with identical parameters.
3. Time-Domain Signal Truncation Issues
Your signal is supposed to hit 0 at 0.5 seconds (corresponding to 2Hz in FFT)—but if your time-domain signal’s length or sampling points don’t match the example, this misalignment will carry over to the frequency domain.
- Confirm your time signal spans exactly 0 to 0.5 seconds, with the correct number of samples:
sample_count = int(Fs * 0.5). Avoid extra samples or truncated ones that might skew the FFT bins.
4. Normalization Discrepancies
Different examples use different FFT normalization rules (dividing by N, sqrt(N), or Fs). While this won’t change the shape of your plot, it can make you think the boundary is off if the amplitude scaling doesn’t match.
- Compare your FFT amplitude calculation to the example. For real signals, a common normalization is
2 * np.abs(fft_result) / Nfor the positive frequency range—make sure your formula matches.
5. Plot Axis Range Settings
Sometimes the issue is just in how you’re displaying the plot, not the FFT itself.
- Manually set your frequency plot’s x-axis to end at 2Hz (e.g.,
plt.xlim(0, 2)if using Matplotlib) and add a vertical reference line at 2Hz to confirm your FFT’s zero point aligns with it.
Quick Example Code to Validate
Here’s a minimal snippet to test your setup with a sinc signal (which has a sharp cutoff at 2Hz, matching your requirement):
import numpy as np import matplotlib.pyplot as plt # Define parameters matching your task Fs = 10 # Sampling frequency (adjust to match your example) t = np.linspace(0, 0.5, int(Fs * 0.5), endpoint=False) # Sinc signal: zero at t=0.5s, corresponding to 2Hz cutoff in FFT signal = np.sinc(2 * (t - 0.25)) # Compute FFT correctly N = len(signal) fft_vals = np.fft.fft(signal) freq_axis = np.fft.fftfreq(N, 1/Fs)[:N//2+1] fft_amp = 2 * np.abs(fft_vals[:N//2+1]) / N # Plot side-by-side fig, (ax_time, ax_fft) = plt.subplots(1, 2, figsize=(12, 4)) ax_time.plot(t, signal) ax_time.set_title('Time Domain Signal') ax_time.set_xlabel('Time (s)') ax_fft.plot(freq_axis, fft_amp) ax_fft.set_title('FFT Amplitude') ax_fft.set_xlabel('Frequency (Hz)') ax_fft.axvline(x=2, color='red', linestyle='--', label='2Hz Cutoff') ax_fft.legend() plt.show()
Run this and check if the red line lines up with the FFT’s zero point—if it does, you can tweak your original code to match this setup!
内容的提问来源于stack exchange,提问作者LutinRose

