### Perform Complex FFT Transformation - FFT.js Source: https://context7.com/indutny/fft.js/llms.txt Performs a forward Fast Fourier Transform on complex input data. Both the input and output must be complex arrays with interleaved real and imaginary components. An error is thrown if the input and output buffers are the same. An example with a cosine wave is provided. ```javascript const FFT = require('fft.js'); const fft = new FFT(4); // Create input signal: [1, 0, -1, 0] in real domain const input = fft.toComplexArray([1, 0, -1, 0]); const output = fft.createComplexArray(); // Perform FFT transformation fft.transform(output, input); console.log(output); // Output: [0, 0, 2, 0, 0, 0, 2, 0] // Format: [real0, imag0, real1, imag1, real2, imag2, real3, imag3] // Error handling: input and output must be different try { fft.transform(input, input); } catch (error) { console.error(error.message); // "Input and output buffers must be different" } // Example with cosine wave at frequency 1 const cosWave = [1, 0.707106, 0, -0.707106]; const cosInput = fft.toComplexArray(cosWave); const cosOutput = fft.createComplexArray(); fft.transform(cosOutput, cosInput); console.log(cosOutput); // Peaks at frequency bins 1 and 3 ``` -------------------------------- ### Initialize FFT Constructor (JavaScript) Source: https://github.com/indutny/fft.js/blob/master/README.md Provides the basic syntax for instantiating the FFT.js class with a specified size. The size must be a power of two and greater than 1. ```javascript const FFT = require('fft.js'); const fft = new FFT(size); ``` -------------------------------- ### Initialize FFT Instance - FFT.js Source: https://context7.com/indutny/fft.js/llms.txt Creates a new FFT instance with a specified size, which must be a power of two greater than 1. The constructor pre-computes trigonometric tables and bit-reversal patterns for performance. Invalid sizes will result in an error. Common valid sizes are demonstrated. ```javascript const FFT = require('fft.js'); // Create FFT instance for 4096 samples const fft = new FFT(4096); // Verify the size console.log(fft.size); // Output: 4096 // Access pre-computed trigonometric table console.log(fft.table.length); // Output: 8192 (size * 2) // Invalid sizes will throw errors try { const invalidFFT = new FFT(100); // Not a power of two } catch (error) { console.error(error.message); // "FFT size must be a power of two and bigger than 1" } // Common valid sizes const fft8 = new FFT(8); const fft256 = new FFT(256); const fft16384 = new FFT(16384); ``` -------------------------------- ### Initialize FFT and Prepare Complex Array (JavaScript) Source: https://github.com/indutny/fft.js/blob/master/README.md Demonstrates how to initialize the FFT.js library with a given size and create a complex array to store data. The complex array uses an interleaved format for real and imaginary parts. ```javascript const FFT = require('fft.js'); const f = new FFT(4096); const input = new Array(4096); input.fill(0); const out = f.createComplexArray(); ``` -------------------------------- ### Complete Spectrum for Real FFT Output (JavaScript) Source: https://github.com/indutny/fft.js/blob/master/README.md Explains how to use `completeSpectrum` to fill the right half of the output array from `realTransform`. This is necessary if the full, symmetric spectrum is required. ```javascript f.completeSpectrum(out); ``` -------------------------------- ### Perform Real FFT (JavaScript) Source: https://github.com/indutny/fft.js/blob/master/README.md Shows how to perform a real Fast Fourier Transform using FFT.js. This is optimized for inputs containing only real numbers and fills only the left half of the output array. ```javascript const realInput = new Array(f.size); f.realTransform(out, realInput); ``` -------------------------------- ### Complete FFT Spectrum (JavaScript) Source: https://github.com/indutny/fft.js/blob/master/README.md Describes the `completeSpectrum` method's functionality, which populates the second half of a complex spectrum array with the complex conjugates of the first half. This is useful after performing a real FFT to obtain the full symmetric spectrum. ```javascript for (every every `i` between 1 and (N / 2 - 1)) spectrum[N - i] = spectrum*[i] ``` -------------------------------- ### Perform Inverse FFT (JavaScript) Source: https://github.com/indutny/fft.js/blob/master/README.md Demonstrates how to compute the inverse Fast Fourier Transform using FFT.js. This method takes the transformed complex data and returns it to the time domain. ```javascript f.inverseTransform(data, out); ``` -------------------------------- ### Perform Complex FFT (JavaScript) Source: https://github.com/indutny/fft.js/blob/master/README.md Illustrates the process of performing a Fast Fourier Transform on complex input data using FFT.js. It first converts the input to a complex array format and then applies the transform. ```javascript const data = f.toComplexArray(input); f.transform(out, data); ``` -------------------------------- ### Optimized Real FFT using fft.js Source: https://context7.com/indutny/fft.js/llms.txt Performs Fast Fourier Transform on real-valued input data, offering improved performance over complex transforms. The output populates only the left half of the spectrum due to inherent symmetry. Requires the 'fft.js' library. ```javascript const FFT = require('fft.js'); const fft = new FFT(2048); // Real input array (no imaginary components) const realInput = new Array(fft.size); for (let i = 0; i < realInput.length; i++) { realInput[i] = Math.sin(2 * Math.PI * i / realInput.length); } // Perform optimized real FFT const output = fft.createComplexArray(); fft.realTransform(output, realInput); // Only left half is filled (right half is symmetric) console.log(output.length); // Output: 4096 (2048 * 2) // Get magnitude of first frequency bin const real0 = output[0]; const imag0 = output[1]; const magnitude0 = Math.sqrt(real0 * real0 + imag0 * imag0); console.log('DC component magnitude:', magnitude0); // Complete the spectrum if full output needed fft.completeSpectrum(output); // Now output contains full symmetric spectrum ``` -------------------------------- ### Inverse FFT using fft.js Source: https://context7.com/indutny/fft.js/llms.txt Transforms frequency domain data back to the time domain using the inverse Fast Fourier Transform. The results are automatically normalized by dividing by the FFT size. Requires the 'fft.js' library. Supports both real and complex inputs. ```javascript const FFT = require('fft.js'); const fft = new FFT(4); // Original signal const original = [1, 0.707106, 0, -0.707106]; // Forward transform const complexInput = fft.toComplexArray(original); const frequencyDomain = fft.createComplexArray(); fft.transform(frequencyDomain, complexInput); console.log('Frequency domain:', frequencyDomain); // Inverse transform back to time domain const timeDomain = fft.createComplexArray(); fft.inverseTransform(timeDomain, frequencyDomain); // Extract real components const reconstructed = fft.fromComplexArray(timeDomain); console.log('Reconstructed:', reconstructed); // Output: [1, 0.707106, 0, -0.707106] // Verify round-trip accuracy for (let i = 0; i < original.length; i++) { const diff = Math.abs(original[i] - reconstructed[i]); console.log(`Index ${i} difference:`, diff); // Should be very small (~0) } // Complete workflow with 256 samples const size = 256; const fft256 = new FFT(size); const signal = new Array(size); for (let i = 0; i < size; i++) { signal[i] = Math.sin(2 * Math.PI * 5 * i / size); // 5 Hz sine wave } const forward = fft256.createComplexArray(); fft256.realTransform(forward, signal); const backward = fft256.createComplexArray(); const forwardFull = fft256.toComplexArray(signal); fft256.transform(forward, forwardFull); fft256.inverseTransform(backward, forward); const result = fft256.fromComplexArray(backward); console.log('Signal reconstructed successfully'); ``` -------------------------------- ### Complete Symmetric Spectrum using fft.js Source: https://context7.com/indutny/fft.js/llms.txt Completes the right half of a spectrum array generated by a real FFT. It utilizes the symmetry property of real FFT output to fill the remaining half with complex conjugates of the left half. Requires the 'fft.js' library. ```javascript const FFT = require('fft.js'); const fft = new FFT(8); // Perform real FFT (fills only left half) const realInput = [1, 2, 3, 4, 5, 6, 7, 8]; const spectrum = fft.createComplexArray(); fft.realTransform(spectrum, realInput); // Before completion: right half is empty console.log('Before:', spectrum.slice(8)); // Contains some values but incomplete // Complete the spectrum fft.completeSpectrum(spectrum); // After completion: full symmetric spectrum // spectrum[N - i] = conjugate of spectrum[i] console.log('After:', spectrum.slice(8)); // Now contains symmetric values // Verify symmetry property const size = fft.size; for (let i = 2; i < size; i += 2) { const leftReal = spectrum[i]; const leftImag = spectrum[i + 1]; const rightReal = spectrum[size * 2 - i]; const rightImag = spectrum[size * 2 - i + 1]; // Real parts should be equal console.log(leftReal === rightReal); // Imaginary parts should be negated console.log(leftImag === -rightImag); } ``` -------------------------------- ### Convert Real Array to Complex Array (JavaScript) Source: https://github.com/indutny/fft.js/blob/master/README.md Explains the `toComplexArray` method, which converts an array of real numbers into the complex array format required by FFT.js. Imaginary parts are initialized to zero. ```javascript const complexArray = f.toComplexArray(realInput, /* optional */ storage); ``` -------------------------------- ### Real Data FFT Transformation Source: https://github.com/indutny/fft.js/blob/master/README.md Performs FFT transformation on an array of real numbers, storing the real part of the complex output in the specified output array. This method is optimized for real input data. ```APIDOC ## `fft.realTransform(output, input)` ### Description Takes an array of real numbers `input` and performs an FFT transformation on it, filling the left half of the `output` array with the real part of the Fourier Transform's complex output. This is the recommended method for real-valued input data as it is approximately 40% faster. ### Method `fft.realTransform` ### Parameters #### Input Parameters - **output** (complex array) - The array where the transformed data will be stored. Should be of size `fft.size` and created using `fft.createComplexArray()`. - **input** (real array) - The array of real numbers to be transformed. Should be of size `fft.size`. ### Request Example ```json { "input": [1, 2, 3, 4, 5, 6, 7, 8], "output": [] } ``` ### Response #### Success Response - The `output` array will be populated with the real part of the complex Fourier Transform. #### Response Example ```json { "output": [complex_part_1, complex_part_2, ...] } ``` ``` -------------------------------- ### Complex Data FFT Transformation Source: https://github.com/indutny/fft.js/blob/master/README.md Performs a forward FFT transformation on a complex input array and stores the result in a complex output array. ```APIDOC ## `fft.transform(output, input)` ### Description Performs a forward Fast Fourier Transform (FFT) on a complex `input` array and stores the resulting complex values in the `output` array. ### Method `fft.transform` ### Parameters #### Input Parameters - **output** (complex array) - The array where the transformed complex data will be stored. Should be of size `fft.size` and created using `fft.createComplexArray()`. - **input** (complex array) - The complex array to be transformed. Should be of size `fft.size` and created using `fft.createComplexArray()`. ### Request Example ```json { "input": [{ "real": 1, "imag": 0 }, { "real": 2, "imag": 0 }, ...], "output": [] } ``` ### Response #### Success Response - The `output` array will be populated with the complex Fourier Transform results. #### Response Example ```json { "output": [{ "real": val1, "imag": val2 }, { "real": val3, "imag": val4 }, ...] } ``` ``` -------------------------------- ### Inverse FFT Transformation Source: https://github.com/indutny/fft.js/blob/master/README.md Performs an inverse Fast Fourier Transform on a complex input array and stores the result in a complex output array. ```APIDOC ## `fft.inverseTransform(output, input)` ### Description Performs an inverse Fast Fourier Transform (IFFT) on a complex `input` array and stores the resulting complex values in the `output` array. This operation reconstructs the original signal from its frequency-domain representation. ### Method `fft.inverseTransform` ### Parameters #### Input Parameters - **output** (complex array) - The array where the inverse transformed complex data will be stored. Should be of size `fft.size` and created using `fft.createComplexArray()`. - **input** (complex array) - The complex array representing the frequency-domain data to be transformed. Should be of size `fft.size` and created using `fft.createComplexArray()`. ### Request Example ```json { "input": [{ "real": val1, "imag": val2 }, { "real": val3, "imag": val4 }, ...], "output": [] } ``` ### Response #### Success Response - The `output` array will be populated with the complex data after the inverse transformation, ideally reconstructing the original time-domain signal. #### Response Example ```json { "output": [{ "real": 1, "imag": 0 }, { "real": 2, "imag": 0 }, ...] } ``` ``` -------------------------------- ### Convert Complex Array to Real Array (JavaScript) Source: https://github.com/indutny/fft.js/blob/master/README.md Details the `fromComplexArray` method, which extracts the real components from a complex array into a standard array. This is a convenience method and ignores imaginary parts. ```javascript const realArray = f.fromComplexArray(complexInput, /* optional */ storage); ``` -------------------------------- ### Create Interleaved Complex Array (JavaScript) Source: https://github.com/indutny/fft.js/blob/master/README.md Shows how to create a complex array suitable for FFT.js operations. The array has a length of `fft.size * 2` and stores real and imaginary parts in an interleaved manner. ```javascript const complexArray = [ real0, imaginary0, real1, imaginary1, ... ]; ``` -------------------------------- ### Convert Real to Complex Array - FFT.js Source: https://context7.com/indutny/fft.js/llms.txt Converts a real-valued array into a complex array format. It sets the real components from the input array and zeroes out all imaginary components. An optional storage array can be provided for memory efficiency, allowing the reuse of an existing array. ```javascript const FFT = require('fft.js'); const fft = new FFT(4); // Convert real input to complex format const realInput = [1, 2, 3, 4]; const complexData = fft.toComplexArray(realInput); console.log(complexData); // Output: [1, 0, 2, 0, 3, 0, 4, 0] // Format: [real0, imag0, real1, imag1, real2, imag2, real3, imag3] // Reuse storage to avoid garbage collection overhead (recommended) const storage = fft.createComplexArray(); const complexData2 = fft.toComplexArray([5, 6, 7, 8], storage); console.log(complexData2); // Output: [5, 0, 6, 0, 7, 0, 8, 0] console.log(storage === complexData2); // Output: true ``` -------------------------------- ### Allocate Complex Array - FFT.js Source: https://context7.com/indutny/fft.js/llms.txt Allocates a complex array with interleaved real and imaginary components. The array is sized to hold the complete FFT output and all its values are initialized to zero. The length of the returned array is always `fft.size * 2`. ```javascript const FFT = require('fft.js'); const fft = new FFT(4); // Create complex array: [real0, imag0, real1, imag1, ...] const complexArray = fft.createComplexArray(); console.log(complexArray.length); // Output: 8 (size * 2) console.log(complexArray); // Output: [0, 0, 0, 0, 0, 0, 0, 0] // Complex arrays always have length = fft.size * 2 // Even indices contain real parts // Odd indices contain imaginary parts ``` -------------------------------- ### Extract Real Components from Complex Array - FFT.js Source: https://context7.com/indutny/fft.js/llms.txt Extracts the real components from a complex array, effectively discarding the imaginary parts. This function is particularly useful for retrieving results after inverse FFT operations. An optional storage array can be provided for memory efficiency. ```javascript const FFT = require('fft.js'); const fft = new FFT(4); // Complex array with real and imaginary parts const complexArray = [1, 0.5, 2, 0.7, 3, 0.2, 4, 0.9]; const realArray = fft.fromComplexArray(complexArray); console.log(realArray); // Output: [1, 2, 3, 4] // Reuse storage for better performance const storage = new Array(fft.size); const realArray2 = fft.fromComplexArray([5, 1, 6, 2, 7, 3, 8, 4], storage); console.log(realArray2); // Output: [5, 6, 7, 8] console.log(storage === realArray2); // Output: true ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.