Wrong Axis Title and Ticks #26

Closed
opened 2024-12-08 17:38:27 +00:00 by nuluh · 1 comment
nuluh commented 2024-12-08 17:38:27 +00:00 (Migrated from github.com)

Maximum Frequency Value After Performing The STFT Appears as 0.5 Instead of 512 Hz

Some STFT implementations (like those in libraries such as SciPy) normalize the frequency axis to a range of [0,0.5], which represents the fraction of the Nyquist frequency relative to the sampling rate.
image

Solution

  • Extract Frequencies and Convert: The frequencies array returned by scipy.signal.stft represents actual frequencies in Hertz if you specify the sampling frequency (fs) in the stft call. Ensure fs is provided correctly.
## Maximum Frequency Value After Performing The STFT Appears as 0.5 Instead of 512 Hz Some STFT implementations (like those in libraries such as SciPy) normalize the frequency axis to a range of $[0,0.5]$, which represents the fraction of the Nyquist frequency relative to the sampling rate. ![image](https://github.com/user-attachments/assets/0cd8122e-3835-494e-ac33-57fdd816a39a) ## Solution - **Extract Frequencies and Convert**: The frequencies array returned by scipy.signal.stft represents actual frequencies in Hertz if you specify the sampling frequency (`fs`) in the `stft` call. Ensure `fs` is provided correctly.
nuluh commented 2025-03-05 11:52:50 +00:00 (Migrated from github.com)

The short explanation is that SciPy’s STFT defaults to a normalized frequency range of $[0, 0.5]$ (cycles per sample) if you don’t provide a sampling rate (fs). This is why you see a maximum frequency of 0.5 instead of 512 Hz.

To fix this, pass your actual sampling frequency (e.g., fs=1024) when calling stft. That way, SciPy will correctly scale the frequency axis up to the Nyquist limit (fs/2 = 512 Hz in your case).

from scipy.signal import stft

# Example fix:
f, t, Zxx = stft(x, fs=1024)  # Now your frequency range will go up to 512 Hz

In summary:

  • No fs provided → Frequencies shown in cycles per sample (max = 0.5).
  • fs=1024 provided → Frequencies shown in Hz (max = 512 Hz).

That’s it! Specifying fs in the STFT call ensures your frequency axis matches real-world units, and you’ll see the maximum frequency you expect.

The short explanation is that **SciPy’s STFT defaults to a normalized frequency range of $[0, 0.5]$** (cycles per sample) if you don’t provide a sampling rate (`fs`). This is why you see a maximum frequency of `0.5` instead of `512 Hz`. To fix this, **pass your actual sampling frequency** (e.g., `fs=1024`) when calling `stft`. That way, SciPy will correctly scale the frequency axis up to the Nyquist limit (`fs/2 = 512 Hz` in your case). ```python from scipy.signal import stft # Example fix: f, t, Zxx = stft(x, fs=1024) # Now your frequency range will go up to 512 Hz ``` **In summary**: - **No `fs` provided** → Frequencies shown in cycles per sample (`max = 0.5`). - **`fs=1024` provided** → Frequencies shown in Hz (`max = 512 Hz`). That’s it! Specifying `fs` in the STFT call ensures your frequency axis matches real-world units, and you’ll see the maximum frequency you expect.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: nuluh/thesis#26