Skip to content

Conversation

nachiket2005
Copy link

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

  • Fully recursive Cooley–Tukey FFT implementation
  • Accepts both numeric and complex input vectors
  • Automatically zero-pads input to the next power of two
  • Returns complex output representing frequency-domain components
  • Compact and easy to understand — ideal for teaching and research use
  • Includes example usage block for quick verification

Complexity

  • Time Complexity: O(N log N)
  • Space Complexity: O(N) (due to recursive calls and intermediate storage)

@Copilot Copilot AI review requested due to automatic review settings October 18, 2025 09:11
Copy link

@Copilot Copilot AI left a 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`.
Copy link

Copilot AI Oct 18, 2025

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.

Suggested change
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)))"
Copy link

Copilot AI Oct 18, 2025

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)))".

Suggested change
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.
Copy link

Copilot AI Oct 18, 2025

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.'

Suggested change
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
Copy link

Copilot AI Oct 18, 2025

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'.

Suggested change
# Example usage when run directly with Rscript
# Example usage in an interactive R session

Copilot uses AI. Check for mistakes.

Comment on lines +43 to +44
# Example usage when run directly with Rscript
if (identical(Sys.getenv("R_SCRIPT_NAME"), "") && interactive()) {
Copy link

Copilot AI Oct 18, 2025

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()) {.

Suggested change
# 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) {
Copy link

Copilot AI Oct 18, 2025

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').

Suggested change
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) {
Copy link

Copilot AI Oct 18, 2025

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)))"
Copy link

Copilot AI Oct 18, 2025

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)))".

Suggested change
# 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant