Spectral
Spectral analysis functions: FFT amplitude spectrum, power spectral density, cross-spectral density, coherence, autocorrelation, and cross-correlation.
All estimators use scipy.signal under the hood with engineering-friendly defaults (Hann window, density scaling, mean detrending).

dspkit.spectral.fft_spectrum(x, fs, window='hann', scaling='amplitude')
Single-sided FFT amplitude spectrum with window amplitude correction.
For a pure sine of amplitude A at frequency f, the returned spectrum will
show A at that frequency bin (with scaling='amplitude').
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
(array_like, shape(N))
|
Time-domain signal. |
required |
fs
|
float
|
Sampling frequency [Hz]. |
required |
window
|
str or None
|
Window function name accepted by |
'hann'
|
scaling
|
(amplitude, rms)
|
|
'amplitude'
|
Returns:
| Name | Type | Description |
|---|---|---|
freqs |
(ndarray, shape(N // 2 + 1))
|
Frequency vector [Hz]. |
amplitude |
(ndarray, shape(N // 2 + 1))
|
Amplitude spectrum in the same units as |
Source code in dspkit/spectral.py
dspkit.spectral.psd(x, fs, window='hann', nperseg=None, noverlap=None, scaling='density', detrend='constant')
Power spectral density (or power spectrum) via Welch's method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
(array_like, shape(N))
|
Time-domain signal. |
required |
fs
|
float
|
Sampling frequency [Hz]. |
required |
window
|
str
|
Window function (default |
'hann'
|
nperseg
|
int or None
|
Segment length. Defaults to |
None
|
noverlap
|
int or None
|
Number of overlapping samples between segments.
Defaults to |
None
|
scaling
|
(density, spectrum)
|
|
'density'
|
detrend
|
str or False
|
Detrending applied to each segment before windowing.
|
'constant'
|
Returns:
| Name | Type | Description |
|---|---|---|
freqs |
ndarray
|
Frequency vector [Hz]. |
Pxx |
ndarray
|
One-sided PSD or power spectrum (real, non-negative). |
Source code in dspkit/spectral.py
dspkit.spectral.csd(x, y, fs, window='hann', nperseg=None, noverlap=None, detrend='constant')
Cross-spectral density via Welch's method.
Gxy(f) = E[X*(f) Y(f)] / Hz, where X, Y are the DFTs of x and y.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
(array_like, shape(N))
|
Input signals. They must have the same sampling frequency. |
required |
y
|
(array_like, shape(N))
|
Input signals. They must have the same sampling frequency. |
required |
fs
|
float
|
Sampling frequency [Hz]. |
required |
window
|
str
|
Window function (default |
'hann'
|
nperseg
|
int or None
|
Segment length. Defaults to |
None
|
noverlap
|
int or None
|
Overlapping samples. Defaults to |
None
|
detrend
|
str or False
|
Per-segment detrending (see |
'constant'
|
Returns:
| Name | Type | Description |
|---|---|---|
freqs |
ndarray
|
Frequency vector [Hz]. |
Pxy |
ndarray(complex)
|
One-sided cross-spectral density [units_x · units_y / Hz]. |
Source code in dspkit/spectral.py
dspkit.spectral.coherence(x, y, fs, window='hann', nperseg=None, noverlap=None, detrend='constant')
Magnitude-squared coherence between x and y.
Cxy(f) = |Gxy(f)|² / (Gxx(f) · Gyy(f)), values in [0, 1].
A value near 1 means the two signals are linearly related at that frequency. A value near 0 indicates noise or nonlinearity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
(array_like, shape(N))
|
|
required |
y
|
(array_like, shape(N))
|
|
required |
fs
|
float
|
Sampling frequency [Hz]. |
required |
window
|
str
|
Window function (default |
'hann'
|
nperseg
|
int or None
|
Segment length. Defaults to |
None
|
noverlap
|
int or None
|
Overlapping samples. Defaults to |
None
|
detrend
|
str or False
|
Per-segment detrending. |
'constant'
|
Returns:
| Name | Type | Description |
|---|---|---|
freqs |
ndarray
|
|
Cxy |
ndarray
|
Magnitude-squared coherence, values in [0, 1]. |
Source code in dspkit/spectral.py
dspkit.spectral.autocorrelation(x, fs=None, normalize=True, max_lag=None)
Biased autocorrelation function (ACF) via FFT.
Uses the biased estimator (divides by N, not N-k) for better variance behaviour at large lags.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
(array_like, shape(N))
|
Input signal (zero-mean recommended; detrend first if needed). |
required |
fs
|
float or None
|
Sampling frequency [Hz]. If provided, the lag axis is in seconds; otherwise it is in samples. |
None
|
normalize
|
bool
|
If |
True
|
max_lag
|
float or None
|
Maximum lag to return. Interpreted in seconds if |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
lags |
ndarray
|
Lag axis (seconds if |
acf |
ndarray
|
Autocorrelation values. |
Source code in dspkit/spectral.py
dspkit.spectral.cross_correlation(x, y, fs=None, normalize=True, max_lag=None)
Biased cross-correlation function (CCF) via FFT.
Computes the full two-sided CCF with lags from -(N-1) to +(N-1):
CCF[k] = (1/N) Σ_n x[n] · y[n + k]
A positive peak at lag k > 0 means y leads x by k samples (i.e. x is a delayed version of y).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
(array_like, shape(N))
|
Input signals. Must have the same length. |
required |
y
|
(array_like, shape(N))
|
Input signals. Must have the same length. |
required |
fs
|
float or None
|
Sampling frequency [Hz]. If provided, the lag axis is in seconds; otherwise it is in samples. |
None
|
normalize
|
bool
|
If |
True
|
max_lag
|
float or None
|
Maximum absolute lag to return. Interpreted in seconds if |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
lags |
ndarray
|
Symmetric lag axis, running from |
ccf |
ndarray
|
Cross-correlation values. |