-
-
Notifications
You must be signed in to change notification settings - Fork 339
Add Fast Fourier Transform (FFT) in R #242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Adds a recursive Cooley–Tukey FFT implementation in R with zero-padding to next power-of-two and accompanying documentation.
- Introduces fft_recursive with power-of-two padding and complex output
- Adds documentation and usage examples
- Includes an interactive example block in the R script
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 8 comments.
File | Description |
---|---|
mathematics/fast_fourier_transform.r | Adds the recursive FFT implementation, zero-padding helper, and an interactive example block |
documentation/fast_fourier_transform.md | Documents the FFT function, usage, and complexity notes |
@@ -0,0 +1,29 @@ | |||
# Fast Fourier Transform (FFT) | |||
|
|||
This file documents the recursive Cooley-Tukey FFT implementation added to `R/mathematics/fast_fourier_transform.r`. |
Copilot
AI
Oct 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The path includes an extra 'R/' segment. Update to mathematics/fast_fourier_transform.r
to match the actual file location.
This file documents the recursive Cooley-Tukey FFT implementation added to `R/mathematics/fast_fourier_transform.r`. | |
This file documents the recursive Cooley-Tukey FFT implementation added to `mathematics/fast_fourier_transform.r`. |
Copilot uses AI. Check for mistakes.
|
||
From the command line with Rscript: | ||
|
||
Rscript -e "source('R/mathematics/fast_fourier_transform.r'); print(fft_recursive(c(0,1,2,3)))" |
Copilot
AI
Oct 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The source path should not include 'R/'. Change to: Rscript -e "source('mathematics/fast_fourier_transform.r'); print(fft_recursive(c(0,1,2,3)))".
Rscript -e "source('R/mathematics/fast_fourier_transform.r'); print(fft_recursive(c(0,1,2,3)))" | |
Rscript -e "source('mathematics/fast_fourier_transform.r'); print(fft_recursive(c(0,1,2,3)))" |
Copilot uses AI. Check for mistakes.
|
||
## Complexity | ||
|
||
Time complexity: O(n log n) for inputs with length a power of two; otherwise dominated by padding to next power of two. |
Copilot
AI
Oct 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The time complexity is not dominated by padding; the runtime is O(M log M) where M is the next power of two (padding itself is O(M−N)). Suggest: 'Time complexity: O(M log M), where M is the next power of two at least as large as the input length.'
Time complexity: O(n log n) for inputs with length a power of two; otherwise dominated by padding to next power of two. | |
Time complexity: O(M log M), where M is the next power of two at least as large as the input length. |
Copilot uses AI. Check for mistakes.
c(even + T, even - T) | ||
} | ||
|
||
# Example usage when run directly with Rscript |
Copilot
AI
Oct 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment does not match the condition below (which only runs in interactive sessions). Update to 'Example usage in an interactive R session'.
# Example usage when run directly with Rscript | |
# Example usage in an interactive R session |
Copilot uses AI. Check for mistakes.
# Example usage when run directly with Rscript | ||
if (identical(Sys.getenv("R_SCRIPT_NAME"), "") && interactive()) { |
Copilot
AI
Oct 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Avoid relying on a non-standard environment variable and simplify the guard; use only interactive(): if (interactive()) {.
# Example usage when run directly with Rscript | |
if (identical(Sys.getenv("R_SCRIPT_NAME"), "") && interactive()) { | |
# Example usage when run directly in an interactive R session | |
if (interactive()) { |
Copilot uses AI. Check for mistakes.
p | ||
} | ||
|
||
fft_recursive <- function(x) { |
Copilot
AI
Oct 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The docs state the function accepts numeric or complex input, but as.complex() will coerce other types silently. Add validation, e.g., before conversion: if (!is.numeric(x) && !is.complex(x)) stop('fft_recursive expects a numeric or complex vector').
fft_recursive <- function(x) { | |
fft_recursive <- function(x) { | |
# Validate input type | |
if (!is.numeric(x) && !is.complex(x)) stop('fft_recursive expects a numeric or complex vector') |
Copilot uses AI. Check for mistakes.
p | ||
} | ||
|
||
fft_recursive <- function(x) { |
Copilot
AI
Oct 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add tests that validate correctness against base::fft for multiple cases (power-of-two length, non-power-of-two input with zero-padding, and complex-valued inputs). This repository requires adding a test or example demonstrating correctness.
Copilot generated this review using guidance from repository custom instructions.
print(fft_recursive(x)) | ||
} | ||
|
||
# When running via Rscript, users can call: Rscript -e "source('R/mathematics/fast_fourier_transform.r'); print(fft_recursive(c(0,1,2,3)))" |
Copilot
AI
Oct 18, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The path should match the file location. Update to: # When running via Rscript, users can call: Rscript -e "source('mathematics/fast_fourier_transform.r'); print(fft_recursive(c(0,1,2,3)))".
# When running via Rscript, users can call: Rscript -e "source('R/mathematics/fast_fourier_transform.r'); print(fft_recursive(c(0,1,2,3)))" | |
# When running via Rscript, users can call: Rscript -e "source('mathematics/fast_fourier_transform.r'); print(fft_recursive(c(0,1,2,3)))" |
Copilot uses AI. Check for mistakes.
Overview
The
fft_recursive
function decomposes a DFT of size N into smaller DFTs of even and odd indexed elements, recursively combining their results.If the input vector’s length is not a power of two, it is zero-padded to the next power of two for optimal performance.
This implementation serves as a clear, educational example of FFT internals while maintaining compatibility with complex input data.
Features
Complexity