Python for Engineers: Analyzing a Vibration Signal with the FFT
Extracting a frequency spectrum from an acceleration signal using NumPy and SciPy. Sampling, windowing and the practical use of the FFT in an engineering context.
Every engineer who works with vibration data eventually wants to move a time signal into the frequency domain. Python offers a powerful, readable environment for this that needs no expensive license. In this article we extract the FFT of an acceleration signal, step by step, with NumPy and SciPy.
Sampling and Nyquist#
It all starts with the sampling rate. The Nyquist theorem states you must sample at least twice as fast as the highest frequency of interest. Otherwise aliasing occurs and high-frequency components fold back into the wrong places in the spectrum.
A practical note
In practice we often use 2.56× the frequency of interest (or more), not just 2×. This leaves room for the anti-aliasing filter and is a standard approach in a real measurement chain.
Generating a Test Signal#
Let's start with a synthetic signal containing two sinusoids and some noise:
import numpy as np
fs = 1000 # sampling rate [Hz]
t = np.arange(0, 1, 1 / fs)
# 50 Hz and 120 Hz components + noise
signal = (
1.0 * np.sin(2 * np.pi * 50 * t)
+ 0.5 * np.sin(2 * np.pi * 120 * t)
+ 0.2 * np.random.randn(len(t))
)Windowing#
The FFT assumes the signal is periodic. A finite record breaks that assumption and causes spectral leakage. A window function (e.g. Hann) reduces this effect:
from scipy.signal import windows
window = windows.hann(len(signal))
windowed = signal * windowFFT and Spectrum#
Let's compute the single-sided amplitude spectrum:
n = len(windowed)
spectrum = np.fft.rfft(windowed)
freqs = np.fft.rfftfreq(n, d=1 / fs)
# amplitude scaling that compensates for the window loss
amplitude = 2 * np.abs(spectrum) / np.sum(window)When you plot freqs against amplitude you should see two clear peaks at 50 Hz and 120 Hz.
Don't forget the scaling
The amplitude scaling depends on the window type. If you change the window you must also update the normalization factor; otherwise the absolute amplitudes will be wrong.
Summary#
Vibration signal analysis in Python comes down to choosing the right sampling rate, applying a suitable window, and scaling the FFT output correctly. This basic workflow forms the core of many applications, from modal testing to machine condition monitoring. In the next article we'll cover computing a PSD (power spectral density) from the same data.