{"id":27154975,"url":"https://github.com/maximilianfeldthusen/fftw","last_synced_at":"2025-06-11T22:33:40.355Z","repository":{"id":281602905,"uuid":"945788245","full_name":"maximilianfeldthusen/fftw","owner":"maximilianfeldthusen","description":"This C++ code generates a sample signal composed of a sine wave with added noise, performs a Fast Fourier Transform (FFT) on that signal, and then displays the magnitude spectrum of the FFT result.","archived":false,"fork":false,"pushed_at":"2025-04-18T07:46:54.000Z","size":17,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"TFD","last_synced_at":"2025-04-18T21:38:02.181Z","etag":null,"topics":["cpp","fft","signal"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/maximilianfeldthusen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-10T06:00:52.000Z","updated_at":"2025-04-18T07:50:05.000Z","dependencies_parsed_at":"2025-03-10T07:20:14.387Z","dependency_job_id":"f771d4f4-80d0-4065-8144-9aaff7c87a0f","html_url":"https://github.com/maximilianfeldthusen/fftw","commit_stats":null,"previous_names":["maximilianfeldthusen/fftw"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/maximilianfeldthusen/fftw","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maximilianfeldthusen%2Ffftw","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maximilianfeldthusen%2Ffftw/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maximilianfeldthusen%2Ffftw/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maximilianfeldthusen%2Ffftw/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/maximilianfeldthusen","download_url":"https://codeload.github.com/maximilianfeldthusen/fftw/tar.gz/refs/heads/TFD","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maximilianfeldthusen%2Ffftw/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259355057,"owners_count":22844981,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","fft","signal"],"created_at":"2025-04-08T18:56:41.105Z","updated_at":"2025-06-11T22:33:40.350Z","avatar_url":"https://github.com/maximilianfeldthusen.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n## Documentation\n\n### **Overview**\n\nThis 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.\n\n---\n\n### **Included Libraries**\n\n- `\u003ciostream\u003e`: for input/output operations.\n- `\u003cvector\u003e`: to handle dynamic arrays.\n- `\u003ccomplex\u003e`: for complex number operations (though not directly used here).\n- `\u003cfftw3.h\u003e`: FFTW library for performing FFTs.\n- `\u003ccmath\u003e`: mathematical functions like `sin()` and `sqrt()`.\n- `\u003ccstdlib\u003e`, `\u003cctime\u003e`, `\u003crandom\u003e`: for random number generation.\n- `\u003calgorithm\u003e`: for functions like `max_element()`.\n\n---\n\n### **Constants and Data Structures**\n\n- `PI`: a constant for π.\n- `struct Peak`: to store information about detected peaks (their index and magnitude).\n\n---\n\n### **Functions**\n\n#### 1. `generateSignal()`\n- **Purpose:** Creates a synthetic signal of length `N` samples containing a sine wave of a specified frequency (`freq`) plus random noise.\n- **Parameters:**\n  - `N`: number of samples.\n  - `freq`: sine wave frequency.\n  - `sampleRate`: sampling rate in Hz.\n  - `generator`: random engine for noise.\n  - `noiseAmplitude`: amplitude of added noise (default 0.5).\n- **Process:**\n  - For each sample `n`, compute `sin(2π * freq * n / sampleRate)`.\n  - Add random noise uniformly distributed between -0.5 and 0.5, scaled by `noiseAmplitude`.\n- **Returns:** a vector containing the noisy sine wave.\n\n---\n\n#### 2. `performFFT()`\n- **Purpose:** Computes the FFT of the input signal and returns the magnitude spectrum.\n- **Process:**\n  - Allocates memory for FFT input (`in`) and output (`out`) arrays using FFTW.\n  - Copies the real input signal into the complex input array (`in`), setting imaginary parts to zero.\n  - Creates an FFT plan and executes it.\n  - Calculates the magnitude of the first half of the FFT output (since the FFT of real signals is symmetric).\n  - Frees FFTW resources.\n- **Returns:** a vector of magnitudes representing the spectrum.\n\n---\n\n#### 3. `detectPeaks()`\n- **Purpose:** Finds local maxima in the spectrum that are above a specified threshold.\n- **Parameters:**\n  - `spectrum`: magnitude spectrum.\n  - `threshold`: minimum magnitude to consider a peak.\n  - `minDistance`: minimum separation between peaks (not fully implemented here).\n- **Process:**\n  - Iterates through the spectrum (excluding first and last points).\n  - Checks if a point is greater than its neighbors and above the threshold.\n  - Collects such points as peaks.\n- **Returns:** a vector of `Peak` objects.\n\n---\n\n#### 4. `displaySpectrum()`\n- **Purpose:** Prints a summarized view of the spectrum, limiting the number of printed points for readability.\n- **Parameters:**\n  - `spectrum`: the magnitude spectrum.\n  - `maxPoints`: maximum number of points to display (default 50).\n\n---\n\n#### 5. `displayPeaks()`\n- **Purpose:** Prints information about the detected peaks.\n\n---\n\n### **Main Function Workflow**\n\n1. **Initialize Random Generator:**\n   - Seeds with current time for noise randomness.\n\n2. **Parameters:**\n   - `sampleRate = 1024 Hz`\n   - `N = 1024 samples`\n   - `freq = 50 Hz` (signal frequency)\n\n3. **Generate Signal:**\n   - Calls `generateSignal()` to create a noisy sine wave.\n\n4. **Compute FFT:**\n   - Calls `performFFT()` to get the spectrum.\n\n5. **Display Spectrum:**\n   - Calls `displaySpectrum()` to print a summarized spectrum.\n\n6. **Peak Detection:**\n   - Finds the maximum magnitude in the spectrum.\n   - Sets a threshold at 30% of this maximum.\n   - Calls `detectPeaks()` to find peaks above this threshold.\n\n7. **Display Detected Peaks:**\n   - Calls `displayPeaks()`.\n\n---\n\n### **Summary**\n\nThis code performs a basic spectral analysis:\n- Generates a noisy sine wave signal.\n- Uses FFT to analyze its frequency content.\n- Detects prominent peaks in the spectrum, which correspond to significant frequency components.\n- Outputs the spectrum and the peaks for inspection.\n\nThis approach is common in signal processing applications for identifying dominant frequencies in noisy signals.\n\n---\n\n**Note:** To run this code, you need to have FFTW installed on your system and compile with the appropriate flags, e.g., `-lfftw3`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaximilianfeldthusen%2Ffftw","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaximilianfeldthusen%2Ffftw","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaximilianfeldthusen%2Ffftw/lists"}