This program generates a synthetic signal composed of a sine wave with added noise, computes its Fast Fourier Transform (FFT) to analyze its frequency components, detects peaks in the spectrum, and displays the results.
<iostream>: for input/output operations.<vector>: to handle dynamic arrays.<complex>: for complex number operations (though not directly used here).<fftw3.h>: FFTW library for performing FFTs.<cmath>: mathematical functions likesin()andsqrt().<cstdlib>,<ctime>,<random>: for random number generation.<algorithm>: for functions likemax_element().
PI: a constant for π.struct Peak: to store information about detected peaks (their index and magnitude).
- Purpose: Creates a synthetic signal of length
Nsamples containing a sine wave of a specified frequency (freq) plus random noise. - Parameters:
N: number of samples.freq: sine wave frequency.sampleRate: sampling rate in Hz.generator: random engine for noise.noiseAmplitude: amplitude of added noise (default 0.5).
- Process:
- For each sample
n, computesin(2π * freq * n / sampleRate). - Add random noise uniformly distributed between -0.5 and 0.5, scaled by
noiseAmplitude.
- For each sample
- Returns: a vector containing the noisy sine wave.
- Purpose: Computes the FFT of the input signal and returns the magnitude spectrum.
- Process:
- Allocates memory for FFT input (
in) and output (out) arrays using FFTW. - Copies the real input signal into the complex input array (
in), setting imaginary parts to zero. - Creates an FFT plan and executes it.
- Calculates the magnitude of the first half of the FFT output (since the FFT of real signals is symmetric).
- Frees FFTW resources.
- Allocates memory for FFT input (
- Returns: a vector of magnitudes representing the spectrum.
- Purpose: Finds local maxima in the spectrum that are above a specified threshold.
- Parameters:
spectrum: magnitude spectrum.threshold: minimum magnitude to consider a peak.minDistance: minimum separation between peaks (not fully implemented here).
- Process:
- Iterates through the spectrum (excluding first and last points).
- Checks if a point is greater than its neighbors and above the threshold.
- Collects such points as peaks.
- Returns: a vector of
Peakobjects.
- Purpose: Prints a summarized view of the spectrum, limiting the number of printed points for readability.
- Parameters:
spectrum: the magnitude spectrum.maxPoints: maximum number of points to display (default 50).
- Purpose: Prints information about the detected peaks.
-
Initialize Random Generator:
- Seeds with current time for noise randomness.
-
Parameters:
sampleRate = 1024 HzN = 1024 samplesfreq = 50 Hz(signal frequency)
-
Generate Signal:
- Calls
generateSignal()to create a noisy sine wave.
- Calls
-
Compute FFT:
- Calls
performFFT()to get the spectrum.
- Calls
-
Display Spectrum:
- Calls
displaySpectrum()to print a summarized spectrum.
- Calls
-
Peak Detection:
- Finds the maximum magnitude in the spectrum.
- Sets a threshold at 30% of this maximum.
- Calls
detectPeaks()to find peaks above this threshold.
-
Display Detected Peaks:
- Calls
displayPeaks().
- Calls
This code performs a basic spectral analysis:
- Generates a noisy sine wave signal.
- Uses FFT to analyze its frequency content.
- Detects prominent peaks in the spectrum, which correspond to significant frequency components.
- Outputs the spectrum and the peaks for inspection.
This approach is common in signal processing applications for identifying dominant frequencies in noisy signals.
Note: To run this code, you need to have FFTW installed on your system and compile with the appropriate flags, e.g., -lfftw3.