java.lang.Object
pl.polsl.rtsa.service.SignalProcessingService
Service responsible for Digital Signal Processing (DSP) operations.
Handles voltage conversion, windowing, FFT calculation, and statistical analysis of the signal data. This service is thread-safe and can be shared across multiple threads.
Production Notes:
- FFT instances are cached per-size to avoid reallocation overhead
- Zero-padding to 65536 samples provides ~0.015 Hz resolution at 1kHz sample rate
- Hamming window is applied by default to reduce spectral leakage
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate org.jtransforms.fft.DoubleFFT_1Dprivate intprivate double[]private intprivate final AppConfigprivate static final org.slf4j.Loggerprivate static final intMinimum FFT size for adequate frequency resolutionprivate static final doubleMinimum frequency threshold to skip DC leakage (Hz) -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new SignalProcessingService with default configuration.SignalProcessingService(AppConfig config) Creates a new SignalProcessingService with custom configuration. -
Method Summary
Modifier and TypeMethodDescriptionprivate voidapplyHammingWindow(double[] data) Applies a Hamming Window to the data array in-place.double[]applyMovingAverage(double[] data, int windowSize) Applies a simple moving average filter for smoothing.doublecalculateRMS(double[] voltageData) Calculates the Root Mean Square (RMS) of the signal.double[]computeFFT(double[] voltageData) Computes the Magnitude Spectrum of the signal using FFT.double[]computeFrequencyAxis(int fftBinCount, double sampleRate) Calculates the frequency axis values for FFT output.computeStatistics(double[] voltageData, double[] freqData, double sampleRate) Computes comprehensive signal statistics.double[]convertToDecibels(double[] magnitude, double floorDb) Converts magnitude to decibels (dB) with floor limiting.double[]convertToVoltage(int[] rawData) Converts raw ADC values (0-1023) to voltage levels (0.0-5.0V).double[]downsampleForDisplay(double[] data, int targetPoints) Downsamples data for display purposes.private double[]getWindowCoefficients(int size) Gets or computes Hamming window coefficients.double[]normalizeMagnitude(double[] magnitude) Normalizes FFT magnitude data for display.double[]removeDCOffset(double[] data) Removes DC offset from the signal.
-
Field Details
-
logger
private static final org.slf4j.Logger logger -
MIN_FFT_SIZE
private static final int MIN_FFT_SIZEMinimum FFT size for adequate frequency resolution- See Also:
-
MIN_FREQ_THRESHOLD
private static final double MIN_FREQ_THRESHOLDMinimum frequency threshold to skip DC leakage (Hz)- See Also:
-
config
-
cachedFFT
private org.jtransforms.fft.DoubleFFT_1D cachedFFT -
cachedFFTSize
private int cachedFFTSize -
cachedWindow
private double[] cachedWindow -
cachedWindowSize
private int cachedWindowSize
-
-
Constructor Details
-
SignalProcessingService
public SignalProcessingService()Creates a new SignalProcessingService with default configuration. -
SignalProcessingService
Creates a new SignalProcessingService with custom configuration.- Parameters:
config- The application configuration to use.
-
-
Method Details
-
convertToVoltage
public double[] convertToVoltage(int[] rawData) Converts raw ADC values (0-1023) to voltage levels (0.0-5.0V).- Parameters:
rawData- The array of raw integer ADC samples.- Returns:
- An array of voltage values.
-
calculateRMS
public double calculateRMS(double[] voltageData) Calculates the Root Mean Square (RMS) of the signal.RMS = sqrt( (1/N) * sum(x[i]^2) )
- Parameters:
voltageData- The time-domain voltage samples.- Returns:
- The calculated RMS value.
-
computeFFT
public double[] computeFFT(double[] voltageData) Computes the Magnitude Spectrum of the signal using FFT.This method automatically applies a Hamming window to the input data to reduce spectral leakage before performing the transform.
- Parameters:
voltageData- The time-domain voltage samples.- Returns:
- An array representing the magnitude of the frequency bins (size N/2).
-
applyHammingWindow
private void applyHammingWindow(double[] data) Applies a Hamming Window to the data array in-place.Formula: w(n) = 0.54 - 0.46 * cos(2 * PI * n / (N - 1))
- Parameters:
data- The data array to modify.
-
getWindowCoefficients
private double[] getWindowCoefficients(int size) Gets or computes Hamming window coefficients. Cached to avoid repeated computation.- Parameters:
size- The window size.- Returns:
- Array of window coefficients.
-
computeStatistics
public SignalStatistics computeStatistics(double[] voltageData, double[] freqData, double sampleRate) Computes comprehensive signal statistics.- Parameters:
voltageData- Time-domain voltage samples.freqData- Frequency-domain magnitude data.sampleRate- Sampling rate in Hz.- Returns:
- SignalStatistics containing all computed metrics.
-
computeFrequencyAxis
public double[] computeFrequencyAxis(int fftBinCount, double sampleRate) Calculates the frequency axis values for FFT output.- Parameters:
fftBinCount- Number of FFT bins (spectrum length).sampleRate- Sampling rate in Hz.- Returns:
- Array of frequency values in Hz for each bin.
-
convertToDecibels
public double[] convertToDecibels(double[] magnitude, double floorDb) Converts magnitude to decibels (dB) with floor limiting.- Parameters:
magnitude- Linear magnitude values.floorDb- Minimum dB value (noise floor).- Returns:
- Array of magnitude values in dB.
-
normalizeMagnitude
public double[] normalizeMagnitude(double[] magnitude) Normalizes FFT magnitude data for display.- Parameters:
magnitude- Raw magnitude values.- Returns:
- Normalized magnitude values (0.0 to 1.0).
-
applyMovingAverage
public double[] applyMovingAverage(double[] data, int windowSize) Applies a simple moving average filter for smoothing.- Parameters:
data- Input data.windowSize- Number of samples to average.- Returns:
- Smoothed data.
-
removeDCOffset
public double[] removeDCOffset(double[] data) Removes DC offset from the signal.- Parameters:
data- Input voltage data.- Returns:
- DC-removed signal (zero-mean).
-
downsampleForDisplay
public double[] downsampleForDisplay(double[] data, int targetPoints) Downsamples data for display purposes.- Parameters:
data- Input data array.targetPoints- Maximum number of output points.- Returns:
- Downsampled data preserving min/max envelope.
-