Code Monkey home page Code Monkey logo

node-fft's Introduction

fft-js

Pure Node.js implementation of the Fast Fourier Transform (Cooley-Tukey Method).

Note: normally inclusion of 'js' in the package name is discouraged. However many of the FFT implementations on NPM at the time this was written are wrappers for other languages like Ruby or C. We wanted to write our own, unoptimized implementation in pure Javascript as an educational tool. As a result it is called fft-js.

Simple Example

var fft = require('fft-js').fft,
    signal = [1,0,1,0];

var phasors = fft(signal);

console.log(phasors);

Frequency/Magnitude Example

var fft = require('fft-js').fft,
    fftUtil = require('fft-js').util,
    signal = [1,0,1,0];

var phasors= fft(signal);

var frequencies = fftUtil.fftFreq(phasors, 8000), // Sample rate and coef is just used for length, and frequency step
    magnitudes = fftUtil.fftMag(phasors); 

var both = frequencies.map(function (f, ix) {
    return {frequency: f, magnitude: magnitudes[ix]};
});

console.log(both);

Calculate IFFT of given phasors

var ifft = require('fft-js').ifft;

var phasors=[[1,0], [0,0], [1,0], [0,0]];

var signal=ifft(phasors);

console.log(signal);

#Calculate fft, modify phasors and calculate ifft

var fft = require('fft-js').fft;
var ifft = require('fft-js').ifft;

var signal=[1,0,1,0];

var phasors=fft(signal);

console.log(phasors);

phasors[2][0]=0;

var signal2=ifft(phasors);

console.log(signal2);

Brute force O(n^2) DFT Example

The DFT (Discrete Fourier Transform) is an unoptimized Fourier Transform for discrete data. In this project it is primarily a teaching tool, and is used to test the FFT.

var dft = require('fft-js').dft,
    signal = [1,0,1,0];

var phasors = dft(signal);

console.log(phasors);

Calculate IDFT of given phasors

var idft = require('fft-js').idft;

var phasors=[[1,0], [0,0], [1,0], [0,0]];

var signal=idft(phasors);

console.log(signal);

In-place FFT Example

The in-place FFT implementation generates the FFT in-place, overwriting the original input vector. This is useful for minimizing new memory allocations required for the recursive version.

var fftInPlace = require('fft-js').fftInPlace,
    signal = [1, 0, 1, 0];
    
fftInPlace(signal);

console.log(signal); //We have overwritten the original vector here with the FFT output.

Command Line

For testing, you can run from the command line. Input is assumed to be from standard input and contain a comma-delimited list of real numbers.

Command: node fft.js -s 44100 test/signal8.js

Console:

Signal:  [ 1, 1, 1, 1, 0, 0, 0, 0 ]

FFT:  [ [ 4, 0 ],
[ 1, -2.414213562373095 ],
[ 0, 0 ],
[ 1, -0.4142135623730949 ],
[ 0, 0 ],
[ 0.9999999999999999, 0.4142135623730949 ],
[ 0, 0 ],
[ 0.9999999999999997, 2.414213562373095 ] ]

FFT Magnitudes:  [ 4, 2.613125929752753, 0, 1.0823922002923938 ] //We only see the first 4, because the 2nd 4 are the Nyquist frequency (discarded for aliasing), and then the mirror image negative frequencies.

FFT Frequencies:  [ 0, 5512.5, 11025, 16537.5 ]

Testing

See test/test.js. Using Mocha:

mocha

Output:

  FFT (Cooley-Tukey)
    1,0,1,0
      √ Should properly compute [1,0,1,0]
    1,0,1,0,2,0,2,0
      √ Should properly compute [1,0,1,0,2,0,2,0]

  IFFT (Cooley-Tukey)
    1,0,1,0
      √ Should properly compute [1,0,1,0]
    1,0,1,0,2,0,2,0
      √ Should properly compute [1,0,1,0,2,0,2,0]

  FFT (in-place Cooley-Tukey)
    1,0,1,0
      √ Should properly compute [1,0,1,0]
    1,0,1,0,2,0,2,0
      √ Should properly compute [1,0,1,0,2,0,2,0]

  DFT O(n^2) Brute Force
    1,0,1,0
      √ Should properly compute [1, 0, 1, 0]

  IDFT O(n^2) Brute Force
    1,0,1,0
      √ Should properly compute [1, 0, 1, 0]
    1,0,1,0,2,0,2,0
      √ Should properly compute [1,0,1,0,2,0,2,0]

  Compare FFT to DFT
    randomSignal FFT
      √ Should compute randomSignal
    randomSignal in-place FFT
      √ Should compute randomSignal
    randomSignal DFT
      √ Should compute randomSignal
    randomSignal FFT and DFT
      √ Should compute same output
    randomSignal in-place FFT and DFT
      √ Should compute same output


  14 passing (27ms)

License

The MIT License (MIT)

Copyright (c) 2015 Vail Systems

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

node-fft's People

Contributors

joshjung avatar maximilianbuegler avatar prior99 avatar waxspin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-fft's Issues

Question: Plausability Output node-fft / Audiospectrogram

Hi, I want to analyze audio data and I want to retrieve frequency/amplitude over time.

  1. When I try this with a 440HZ sine Wave I would expect the highest amplitude at 440Hz.
  2. How are the other output frequencies chosen? Can I get the results e.g. in a frequency range, like 0Hz-20Khz?
const debugWav = require('debug')('wav');

const fs = require('fs');
const fft = require('fft-js').fft;
const fftUtil = require('fft-js').util;
const sine = require('audio-oscillator/sin');
const {unpackArray} = require('byte-data');


const samplesLength = 1024;
let sampleRate = 44100; // wav 44100 sampleRate, 16bit

/**
 * Generate a sine wave
 */
const generateWav = () => {
    // generate sine wave 440 Hz
    const waveform = sine(samplesLength, 440); // samples, frequency
    return waveform
};


/**
 * Process the wav
 */

const phasors = fft(generateWav());

const frequencies = fftUtil.fftFreq(phasors, 44100), // Sample rate and coef is just used for length, and frequency step
    magnitudes = fftUtil.fftMag(phasors);

const both = frequencies.map(function (f, ix) {
    return {frequency: f, magnitude: magnitudes[ix]};
});

console.table(both);

Output:

┌─────────┬────────────────┬────────────────────┐
│ (index) │   frequency    │     magnitude      │
├─────────┼────────────────┼────────────────────┤
│    0    │       0        │ 12.152954611168916 │
│    1    │  43.06640625   │ 12.371840124428138 │
│    2    │   86.1328125   │ 13.049660038632785 │
│    3    │  129.19921875  │ 14.257748464539002 │
│    4    │   172.265625   │ 16.145493015641954 │
│    5    │  215.33203125  │ 19.006782054318574 │
│    6    │  258.3984375   │ 23.444013431023404 │
│    7    │  301.46484375  │ 30.839530345061696 │
│    8    │   344.53125    │ 45.104565448543774 │
│    9    │  387.59765625  │ 83.05529774944604  │
│   10    │  430.6640625   │ 471.99329683509916 │
│   11    │  473.73046875  │ 132.38812248080427 │
│   12    │   516.796875   │ 58.94894310683054  │
│   13    │  559.86328125  │ 38.29097615220791  │
│   14    │  602.9296875   │ 28.55339797808452  │
│   15    │  645.99609375  │  22.8834459943328  │
│   16    │    689.0625    │ 19.169092636055744 │
│   17    │  732.12890625  │ 16.544346462620222 │
│   18    │  775.1953125   │ 14.588639163496254 │
│   19    │  818.26171875  │ 13.073165838642582 │
│   20    │   861.328125   │ 11.862817675507356 │
│   21    │  904.39453125  │ 10.872696852206447 │
│   22    │  947.4609375   │ 10.04678419250667  │
│   23    │  990.52734375  │ 9.346626225654326  │
│   24    │   1033.59375   │ 8.744951406922706  │
│   25    │ 1076.66015625  │ 8.221880329631402  │
│   26    │  1119.7265625  │ 7.762578689775234  │
│   27    │ 1162.79296875  │ 7.355750727231507  │
│   28    │  1205.859375   │ 6.992641946837161  │
│   29    │ 1248.92578125  │ 6.666361076689498  │
│   30    │  1291.9921875  │ 6.371408134738898  │
│   31    │ 1335.05859375  │ 6.103339064749246  │
│   32    │    1378.125    │ 5.858522971508816  │
│   33    │ 1421.19140625  │ 5.633963445745503  │
│   34    │  1464.2578125  │ 5.427165073795851  │
│   35    │ 1507.32421875  │ 5.236032340013399  │
│   36    │  1550.390625   │ 5.058792106400648  │
│   37    │ 1593.45703125  │ 4.893933492369614  │
│   38    │  1636.5234375  │  4.74016075996692  │
│   39    │ 1679.58984375  │ 4.596356034064684  │
│   40    │   1722.65625   │ 4.461549540593798  │
│   41    │ 1765.72265625  │ 4.334895649436749  │
│   42    │  1808.7890625  │  4.21565344088737  │
│   43    │ 1851.85546875  │  4.10317082794807  │
│   44    │  1894.921875   │ 3.9968714964351753 │
│   45    │ 1937.98828125  │ 3.896244094995214  │
│   46    │  1981.0546875  │ 3.8008332343806863 │
│   47    │ 2024.12109375  │ 3.710231951372898  │
│   48    │   2067.1875    │ 3.624075365850696  │
│   49    │ 2110.25390625  │ 3.5420353156097173 │
│   50    │  2153.3203125  │ 3.463815796922526  │
│   51    │ 2196.38671875  │ 3.3891490726204667 │
│   52    │  2239.453125   │ 3.317792335974296  │
│   53    │ 2282.51953125  │ 3.2495248395633305 │
│   54    │  2325.5859375  │ 3.184145414927604  │
│   55    │ 2368.65234375  │ 3.121470322059914  │
│   56    │   2411.71875   │ 3.0613313784448493 │
│   57    │ 2454.78515625  │ 3.0035743259530703 │
│   58    │  2497.8515625  │ 2.9480574008737856 │
│   59    │ 2540.91796875  │ 2.8946500780591413 │
│   60    │  2583.984375   │ 2.843231964813001  │
│   61    │ 2627.05078125  │ 2.7936918239895956 │
│   62    │  2670.1171875  │ 2.7459267089345327 │
│   63    │ 2713.18359375  │ 2.6998411955269557 │
│   64    │    2756.25     │  2.65534669876918  │
│   65    │ 2799.31640625  │ 2.6123608631974977 │
│   66    │  2842.3828125  │ 2.570807017921727  │
│   67    │ 2885.44921875  │ 2.5306136883907895 │
│   68    │  2928.515625   │ 2.491714158071842  │
│   69    │ 2971.58203125  │ 2.4540460741530628 │
│   70    │  3014.6484375  │ 2.4175510921649352 │
│   71    │ 3057.71484375  │ 2.3821745550828988 │
│   72    │   3100.78125   │ 2.3478652030460068 │
│   73    │ 3143.84765625  │ 2.3145749103153315 │
│   74    │  3186.9140625  │ 2.282258446516979  │
│   75    │ 3229.98046875  │ 2.2508732595769505 │
│   76    │  3273.046875   │ 2.220379278067911  │
│   77    │ 3316.11328125  │ 2.1907387309594126 │
│   78    │  3359.1796875  │ 2.1619159829977477 │
│   79    │ 3402.24609375  │ 2.1338773841468113 │
│   80    │   3445.3125    │ 2.1065911316990023 │
│   81    │ 3488.37890625  │ 2.0800271438214692 │
│   82    │  3531.4453125  │ 2.054156943439062  │
│   83    │ 3574.51171875  │ 2.0289535514749724 │
│   84    │  3617.578125   │ 2.0043913885751485 │
│   85    │ 3660.64453125  │ 1.9804461845350614 │
│   86    │  3703.7109375  │ 1.9570948947290032 │
│   87    │ 3746.77734375  │ 1.9343156229142768 │
│   88    │   3789.84375   │ 1.9120875498459815 │
│   89    │ 3832.91015625  │ 1.8903908671957874 │
│   90    │  3875.9765625  │ 1.8692067163169377 │
│   91    │ 3919.04296875  │ 1.8485171314433517 │
│   92    │  3962.109375   │ 1.8283049869505235 │
│   93    │ 4005.17578125  │ 1.8085539483406927 │
│   94    │  4048.2421875  │ 1.789248426647436  │
│   95    │ 4091.30859375  │ 1.770373535982654  │
│   96    │    4134.375    │ 1.7519150539745088 │
│   97    │ 4177.44140625  │ 1.733859384867805  │
│   98    │  4220.5078125  │ 1.7161935250787304 │
│   99    │ 4263.57421875  │ 1.6989050310145166 │
│   100   │  4306.640625   │ 1.6819819889848078 │
│   101   │ 4349.70703125  │ 1.6654129870471555 │
│   102   │  4392.7734375  │ 1.649187088641961  │
│   103   │ 4435.83984375  │ 1.6332938078849353 │
│   104   │   4478.90625   │ 1.6177230863958667 │
│   105   │ 4521.97265625  │ 1.6024652715528576 │
│   106   │  4565.0390625  │ 1.5875110960700973 │
│   107   │ 4608.10546875  │ 1.5728516588054446 │
│   108   │  4651.171875   │ 1.5584784067120494 │
│   109   │ 4694.23828125  │ 1.5443831178544063 │
│   110   │  4737.3046875  │ 1.5305578854161885 │
│   111   │ 4780.37109375  │ 1.5169951026323973 │
│   112   │   4823.4375    │ 1.503687448583734  │
│   113   │ 4866.50390625  │ 1.4906278747959294 │
│   114   │  4909.5703125  │ 1.4778095925909394 │
│   115   │ 4952.63671875  │ 1.465226061140952  │
│   116   │  4995.703125   │ 1.452870976179976  │
│   117   │ 5038.76953125  │ 1.4407382593305655 │
│   118   │  5081.8359375  │ 1.4288220480072211 │
│   119   │ 5124.90234375  │ 1.4171166858597923 │
│   120   │   5167.96875   │ 1.4056167137234505 │
│   121   │ 5211.03515625  │ 1.3943168610441796 │
│   122   │  5254.1015625  │ 1.383212037750449  │
│   123   │ 5297.16796875  │ 1.3722973265443272 │
│   124   │  5340.234375   │ 1.3615679755866048 │
│   125   │ 5383.30078125  │ 1.3510193915528388 │
│   126   │  5426.3671875  │ 1.3406471330380145 │
│   127   │ 5469.43359375  │ 1.3304469042899936 │
│   128   │     5512.5     │ 1.3204145492521742 │
│   129   │ 5555.56640625  │ 1.310546045897987  │
│   130   │  5598.6328125  │ 1.3008375008404036 │
│   131   │ 5641.69921875  │ 1.291285144201002  │
│   132   │  5684.765625   │ 1.2818853247240856 │
│   133   │ 5727.83203125  │ 1.2726345051221066 │
│   134   │  5770.8984375  │ 1.2635292576398605 │
│   135   │ 5813.96484375  │ 1.2545662598252671 │
│   136   │   5857.03125   │ 1.2457422904957558 │
│   137   │ 5900.09765625  │ 1.2370542258895871 │
│   138   │  5943.1640625  │ 1.2284990359922503 │
│   139   │ 5986.23046875  │ 1.2200737810288795 │
│   140   │  6029.296875   │ 1.2117756081135687 │
│   141   │ 6072.36328125  │ 1.2036017480478065 │
│   142   │  6115.4296875  │ 1.1955495122601592 │
│   143   │ 6158.49609375  │ 1.1876162898798384 │
│   144   │   6201.5625    │ 1.1797995449375729 │
│   145   │ 6244.62890625  │ 1.1720968136870935 │
│   146   │  6287.6953125  │ 1.164505702041419  │
│   147   │ 6330.76171875  │ 1.1570238831180508 │
│   148   │  6373.828125   │ 1.1496490948877365 │
│   149   │ 6416.89453125  │ 1.1423791379217956 │
│   150   │  6459.9609375  │ 1.1352118732330423 │
│   151   │ 6503.02734375  │ 1.1281452202060451 │
│   152   │   6546.09375   │ 1.121177154611966  │
│   153   │ 6589.16015625  │ 1.1143057067045552 │
│   154   │  6632.2265625  │ 1.1075289593929885 │
│   155   │ 6675.29296875  │ 1.1008450464880004 │
│   156   │  6718.359375   │ 1.0942521510181538 │
│   157   │ 6761.42578125  │ 1.0877485036126562 │
│   158   │  6804.4921875  │ 1.0813323809478814 │
│   159   │ 6847.55859375  │ 1.0750021042545954 │
│   160   │    6890.625    │ 1.0687560378830854 │
│   161   │ 6933.69140625  │ 1.0625925879237053 │
│   162   │  6976.7578125  │ 1.0565102008801939 │
│   163   │ 7019.82421875  │ 1.0505073623935723 │
│   164   │  7062.890625   │ 1.0445825960142023 │
│   165   │ 7105.95703125  │ 1.038734462020084  │
│   166   │  7149.0234375  │ 1.0329615562791732 │
│   167   │ 7192.08984375  │ 1.0272625091539958 │
│   168   │   7235.15625   │ 1.0216359844465652 │
│   169   │ 7278.22265625  │ 1.0160806783819898 │
│   170   │  7321.2890625  │ 1.0105953186290697 │
│   171   │ 7364.35546875  │ 1.0051786633563107 │
│   172   │  7407.421875   │ 0.999829500321966  │
│   173   │ 7450.48828125  │ 0.994546645996437  │
│   174   │  7493.5546875  │ 0.9893289447159996 │
│   175   │ 7536.62109375  │ 0.9841752678663415 │
│   176   │   7579.6875    │ 0.979084513094743  │
│   177   │ 7622.75390625  │ 0.9740556035497749 │
│   178   │  7665.8203125  │ 0.9690874871473424 │
│   179   │ 7708.88671875  │ 0.9641791358620374 │
│   180   │  7751.953125   │ 0.9593295450427901 │
│   181   │ 7795.01953125  │ 0.9545377327518263 │
│   182   │  7838.0859375  │ 0.9498027391260084 │
│   183   │ 7881.15234375  │ 0.9451236257597078 │
│   184   │   7924.21875   │ 0.9404994751083671 │
│   185   │ 7967.28515625  │ 0.9359293899118848 │
│   186   │  8010.3515625  │ 0.9314124926370905 │
│   187   │ 8053.41796875  │ 0.9269479249386134 │
│   188   │  8096.484375   │ 0.922534847137409  │
│   189   │ 8139.55078125  │ 0.9181724377161812 │
│   190   │  8182.6171875  │ 0.9138598928313305 │
│   191   │ 8225.68359375  │ 0.9095964258403147 │
│   192   │    8268.75     │ 0.9053812668445473 │
│   193   │ 8311.81640625  │ 0.9012136622464734 │
│   194   │  8354.8828125  │ 0.8970928743209342 │
│   195   │ 8397.94921875  │ 0.8930181807999668 │
│   196   │  8441.015625   │ 0.8889888744705485 │
│   197   │ 8484.08203125  │ 0.8850042627850117 │
│   198   │  8527.1484375  │ 0.8810636674834446 │
│   199   │ 8570.21484375  │ 0.8771664242277897 │
│   200   │   8613.28125   │ 0.8733118822471652 │
│   201   │ 8656.34765625  │ 0.8694994039939781 │
│   202   │  8699.4140625  │ 0.8657283648106044 │
│   203   │ 8742.48046875  │ 0.8619981526059125 │
│   204   │  8785.546875   │ 0.8583081675418567 │
│   205   │ 8828.61328125  │ 0.8546578217290682 │
│   206   │  8871.6796875  │ 0.8510465389317811 │
│   207   │ 8914.74609375  │ 0.8474737542813985 │
│   208   │   8957.8125    │ 0.8439389139984876 │
│   209   │ 9000.87890625  │ 0.8404414751229206 │
│   210   │  9043.9453125  │ 0.8369809052519736 │
│   211   │ 9087.01171875  │ 0.8335566822859806 │
│   212   │  9130.078125   │ 0.8301682941811723 │
│   213   │ 9173.14453125  │ 0.8268152387099789 │
│   214   │  9216.2109375  │ 0.8234970232278401 │
│   215   │ 9259.27734375  │ 0.820213164446897  │
│   216   │   9302.34375   │ 0.8169631882159036 │
│   217   │ 9345.41015625  │ 0.813746629306537  │
│   218   │  9388.4765625  │ 0.8105630312055566 │
│   219   │ 9431.54296875  │ 0.8074119459128286 │
│   220   │  9474.609375   │ 0.8042929337449944 │
│   221   │ 9517.67578125  │ 0.8012055631444901 │
│   222   │  9560.7421875  │ 0.7981494104939528 │
│   223   │ 9603.80859375  │ 0.7951240599355628 │
│   224   │    9646.875    │ 0.7921291031954435 │
│   225   │ 9689.94140625  │ 0.7891641394127583 │
│   226   │  9733.0078125  │ 0.7862287749734875 │
│   227   │ 9776.07421875  │ 0.783322623348609  │
│   228   │  9819.140625   │ 0.7804453049366743 │
│   229   │ 9862.20703125  │ 0.7775964469105909 │
│   230   │  9905.2734375  │ 0.7747756830683817 │
│   231   │ 9948.33984375  │ 0.7719826536880043 │
│   232   │   9991.40625   │ 0.7692170053859664 │
│   233   │ 10034.47265625 │ 0.7664783909795868 │
│   234   │ 10077.5390625  │ 0.7637664693528738 │
│   235   │ 10120.60546875 │ 0.7610809053259505 │
│   236   │  10163.671875  │ 0.7584213695278121 │
│   237   │ 10206.73828125 │ 0.7557875382722999 │
│   238   │ 10249.8046875  │ 0.7531790934373814 │
│   239   │ 10292.87109375 │ 0.7505957223474149 │
│   240   │   10335.9375   │ 0.7480371176584374 │
│   241   │ 10379.00390625 │ 0.745502977246369  │
│   242   │ 10422.0703125  │ 0.7429930040980557 │
│   243   │ 10465.13671875 │ 0.7405069062049577 │
│   244   │  10508.203125  │ 0.738044396459589  │
│   245   │ 10551.26953125 │ 0.7356051925545116 │
│   246   │ 10594.3359375  │ 0.7331890168837784 │
│   247   │ 10637.40234375 │ 0.7307955964470021 │
│   248   │  10680.46875   │ 0.7284246627553796 │
│   249   │ 10723.53515625 │ 0.7260759517404931 │
│   250   │ 10766.6015625  │  0.72374920366505  │
│   251   │ 10809.66796875 │ 0.7214441630358492 │
│   252   │  10852.734375  │ 0.7191605785188439 │
│   253   │ 10895.80078125 │ 0.716898202856309  │
│   254   │ 10938.8671875  │ 0.7146567927859353 │
│   255   │ 10981.93359375 │ 0.712436108961887  │
│   256   │     11025      │ 0.7102359158777101 │
│   257   │ 11068.06640625 │ 0.7080559817911036 │
│   258   │ 11111.1328125  │ 0.7058960786504748 │
│   259   │ 11154.19921875 │ 0.7037559820231458 │
│   260   │  11197.265625  │ 0.7016354710253743 │
│   261   │ 11240.33203125 │ 0.6995343282538382 │
│   262   │ 11283.3984375  │ 0.6974523397188923 │
│   263   │ 11326.46484375 │ 0.6953892947792317 │
│   264   │  11369.53125   │ 0.6933449860781569 │
│   265   │ 11412.59765625 │ 0.6913192094812238 │
│   266   │ 11455.6640625  │ 0.6893117640154295 │
│   267   │ 11498.73046875 │ 0.6873224518095441 │
│   268   │  11541.796875  │ 0.6853510780362898 │
│   269   │ 11584.86328125 │ 0.6833974508552063 │
│   270   │ 11627.9296875  │ 0.6814613813572552 │
│   271   │ 11670.99609375 │ 0.6795426835105075 │
│   272   │   11714.0625   │ 0.677641174107065  │
│   273   │ 11757.12890625 │ 0.6757566727111779 │
│   274   │ 11800.1953125  │ 0.6738890016084861 │
│   275   │ 11843.26171875 │ 0.6720379857564167 │
│   276   │  11886.328125  │ 0.6702034527356622 │
│   277   │ 11929.39453125 │ 0.6683852327027254 │
│   278   │ 11972.4609375  │ 0.6665831583435046 │
│   279   │ 12015.52734375 │ 0.6647970648279368 │
│   280   │  12058.59375   │ 0.663026789765516  │
│   281   │ 12101.66015625 │ 0.661272173161877  │
│   282   │ 12144.7265625  │ 0.6595330573763096 │
│   283   │ 12187.79296875 │ 0.6578092870801286 │
│   284   │  12230.859375  │ 0.6561007092160064 │
│   285   │ 12273.92578125 │ 0.6544071729580885 │
│   286   │ 12316.9921875  │ 0.6527285296730775 │
│   287   │ 12360.05859375 │ 0.6510646328820283 │
│   288   │   12403.125    │ 0.6494153382230178 │
│   289   │ 12446.19140625 │ 0.6477805034145884 │
│   290   │ 12489.2578125  │ 0.646159988219951  │
│   291   │ 12532.32421875 │ 0.6445536544119334 │
│   292   │  12575.390625  │ 0.6429613657386952 │
│   293   │ 12618.45703125 │ 0.6413829878901197 │
│   294   │ 12661.5234375  │ 0.6398183884648961 │
│   295   │ 12704.58984375 │ 0.6382674369383002 │
│   296   │  12747.65625   │ 0.6367300046307028 │
│   297   │ 12790.72265625 │ 0.6352059646765789 │
│   298   │ 12833.7890625  │ 0.6336951919942735 │
│   299   │ 12876.85546875 │ 0.6321975632563025 │
│   300   │  12919.921875  │ 0.6307129568604022 │
│   301   │ 12962.98828125 │ 0.6292412529009501 │
│   302   │ 13006.0546875  │ 0.6277823331411194 │
│   303   │ 13049.12109375 │ 0.626336080985555  │
│   304   │   13092.1875   │ 0.6249023814535887 │
│   305   │ 13135.25390625 │ 0.6234811211530132 │
│   306   │ 13178.3203125  │ 0.6220721882543192 │
│   307   │ 13221.38671875 │ 0.6206754724655292 │
│   308   │  13264.453125  │ 0.6192908650074535 │
│   309   │ 13307.51953125 │ 0.6179182585895316 │
│   310   │ 13350.5859375  │ 0.6165575473859886 │
│   311   │ 13393.65234375 │ 0.6152086270126826 │
│   312   │  13436.71875   │ 0.6138713945041759 │
│   313   │ 13479.78515625 │ 0.6125457482914228 │
│   314   │ 13522.8515625  │ 0.6112315881798147 │
│   315   │ 13565.91796875 │ 0.6099288153276872 │
│   316   │  13608.984375  │ 0.6086373322251584 │
│   317   │ 13652.05078125 │ 0.6073570426735272 │
│   318   │ 13695.1171875  │ 0.6060878517649428 │
│   319   │ 13738.18359375 │ 0.6048296658624938 │
│   320   │    13781.25    │ 0.6035823925807031 │
│   321   │ 13824.31640625 │ 0.6023459407663738 │
│   322   │ 13867.3828125  │ 0.6011202204798132 │
│   323   │ 13910.44921875 │ 0.5999051429763976 │
│   324   │  13953.515625  │ 0.5987006206885245 │
│   325   │ 13996.58203125 │ 0.5975065672078168 │
│   326   │ 14039.6484375  │ 0.5963228972677794 │
│   327   │ 14082.71484375 │ 0.5951495267266942 │
│   328   │  14125.78125   │ 0.5939863725508676 │
│   329   │ 14168.84765625 │ 0.592833352798187  │
│   330   │ 14211.9140625  │ 0.5916903866019808 │
│   331   │ 14254.98046875 │ 0.5905573941551976 │
│   332   │  14298.046875  │ 0.5894342966948477 │
│   333   │ 14341.11328125 │ 0.5883210164867453 │
│   334   │ 14384.1796875  │ 0.5872174768105209 │
│   335   │ 14427.24609375 │ 0.5861236019449632 │
│   336   │   14470.3125   │ 0.5850393171535406 │
│   337   │ 14513.37890625 │ 0.5839645486702618 │
│   338   │ 14556.4453125  │ 0.5828992236857622 │
│   339   │ 14599.51171875 │ 0.5818432703336339 │
│   340   │  14642.578125  │ 0.5807966176770423 │
│   341   │ 14685.64453125 │ 0.5797591956955515 │
│   342   │ 14728.7109375  │ 0.5787309352721699 │
│   343   │ 14771.77734375 │ 0.577711768180721  │
│   344   │  14814.84375   │ 0.5767016270733087 │
│   345   │ 14857.91015625 │ 0.5757004454681118 │
│   346   │ 14900.9765625  │ 0.5747081577373679 │
│   347   │ 14944.04296875 │ 0.573724699095535  │
│   348   │  14987.109375  │ 0.5727500055877299 │
│   349   │ 15030.17578125 │ 0.5717840140782923 │
│   350   │ 15073.2421875  │ 0.5708266622396493 │
│   351   │ 15116.30859375 │ 0.569877888541274  │
│   352   │   15159.375    │ 0.5689376322389527 │
│   353   │ 15202.44140625 │ 0.5680058333640758 │
│   354   │ 15245.5078125  │ 0.5670824327133249 │
│   355   │ 15288.57421875 │ 0.5661673718383848 │
│   356   │  15331.640625  │ 0.5652605930358934 │
│   357   │ 15374.70703125 │ 0.5643620393375383 │
│   358   │ 15417.7734375  │ 0.5634716545003883 │
│   359   │ 15460.83984375 │ 0.5625893829973242 │
│   360   │  15503.90625   │ 0.5617151700076936 │
│   361   │ 15546.97265625 │ 0.5608489614080675 │
│   362   │ 15590.0390625  │ 0.5599907037631934 │
│   363   │ 15633.10546875 │ 0.5591403443171454 │
│   364   │  15676.171875  │ 0.5582978309845082 │
│   365   │ 15719.23828125 │ 0.5574631123418404 │
│   366   │ 15762.3046875  │ 0.5566361376192144 │
│   367   │ 15805.37109375 │ 0.5558168566919401 │
│   368   │   15848.4375   │ 0.5550052200723676 │
│   369   │ 15891.50390625 │ 0.5542011789019177 │
│   370   │ 15934.5703125  │ 0.5534046849431536 │
│   371   │ 15977.63671875 │ 0.5526156905720909 │
│   372   │  16020.703125  │ 0.5518341487705557 │
│   373   │ 16063.76953125 │ 0.5510600131187084 │
│   374   │ 16106.8359375  │ 0.5502932377876534 │
│   375   │ 16149.90234375 │ 0.5495337775323104 │
│   376   │  16192.96875   │ 0.5487815876841954 │
│   377   │ 16236.03515625 │ 0.5480366241444631 │
│   378   │ 16279.1015625  │ 0.5472988433771083 │
│   379   │ 16322.16796875 │ 0.546568202402132  │
│   380   │  16365.234375  │ 0.5458446587889152 │
│   381   │ 16408.30078125 │ 0.5451281706497485 │
│   382   │ 16451.3671875  │ 0.544418696633332  │
│   383   │ 16494.43359375 │ 0.543716195918556  │
│   384   │    16537.5     │ 0.5430206282082342 │
│   385   │ 16580.56640625 │ 0.5423319537230257 │
│   386   │ 16623.6328125  │ 0.5416501331954467 │
│   387   │ 16666.69921875 │ 0.5409751278640036 │
│   388   │  16709.765625  │ 0.5403068994672884 │
│   389   │ 16752.83203125 │ 0.539645410238438  │
│   390   │ 16795.8984375  │ 0.5389906228994112 │
│   391   │ 16838.96484375 │ 0.5383425006555281 │
│   392   │  16882.03125   │ 0.537701007190063  │
│   393   │ 16925.09765625 │ 0.5370661066588843 │
│   394   │ 16968.1640625  │ 0.5364377636852565 │
│   395   │ 17011.23046875 │ 0.5358159433546236 │
│   396   │  17054.296875  │ 0.5352006112096642 │
│   397   │ 17097.36328125 │ 0.5345917332452189 │
│   398   │ 17140.4296875  │ 0.5339892759033671 │
│   399   │ 17183.49609375 │ 0.5333932060687556 │
│   400   │   17226.5625   │ 0.5328034910637383 │
│   401   │ 17269.62890625 │ 0.532220098643762  │
│   402   │ 17312.6953125  │ 0.5316429969928009 │
│   403   │ 17355.76171875 │ 0.5310721547188756 │
│   404   │  17398.828125  │ 0.5305075408495719 │
│   405   │ 17441.89453125 │ 0.5299491248277597 │
│   406   │ 17484.9609375  │ 0.5293968765072761 │
│   407   │ 17528.02734375 │ 0.528850766148753  │
│   408   │  17571.09375   │ 0.5283107644154377 │
│   409   │ 17614.16015625 │ 0.5277768423691656 │
│   410   │ 17657.2265625  │ 0.5272489714663892 │
│   411   │ 17700.29296875 │ 0.5267271235541899 │
│   412   │  17743.359375  │ 0.5262112708664668 │
│   413   │ 17786.42578125 │ 0.5257013860201201 │
│   414   │ 17829.4921875  │ 0.5251974420113227 │
│   415   │ 17872.55859375 │ 0.5246994122118466 │
│   416   │   17915.625    │ 0.5242072703654981 │
│   417   │ 17958.69140625 │ 0.5237209905844974 │
│   418   │ 18001.7578125  │ 0.523240547346119  │
│   419   │ 18044.82421875 │ 0.522765915489088  │
│   420   │  18087.890625  │ 0.5222970702103934 │
│   421   │ 18130.95703125 │ 0.5218339870618738 │
│   422   │ 18174.0234375  │ 0.5213766419470219 │
│   423   │ 18217.08984375 │ 0.5209250111177638 │
│   424   │  18260.15625   │ 0.5204790711713162 │
│   425   │ 18303.22265625 │ 0.5200387990471262 │
│   426   │ 18346.2890625  │ 0.5196041720238129 │
│   427   │ 18389.35546875 │ 0.5191751677162276 │
│   428   │  18432.421875  │ 0.5187517640725201 │
│   429   │ 18475.48828125 │ 0.5183339393711869 │
│   430   │ 18518.5546875  │ 0.5179216722183924 │
│   431   │ 18561.62109375 │ 0.5175149415450879 │
│   432   │   18604.6875   │ 0.5171137266043142 │
│   433   │ 18647.75390625 │ 0.5167180069685451 │
│   434   │ 18690.8203125  │ 0.516327762527056  │
│   435   │ 18733.88671875 │ 0.5159429734833246 │
│   436   │  18776.953125  │ 0.5155636203525237 │
│   437   │ 18820.01953125 │ 0.5151896839590351 │
│   438   │ 18863.0859375  │ 0.5148211454339584 │
│   439   │ 18906.15234375 │ 0.5144579862128547 │
│   440   │  18949.21875   │ 0.5141001880331736 │
│   441   │ 18992.28515625 │ 0.513747732932153  │
│   442   │ 19035.3515625  │ 0.5134006032444526 │
│   443   │ 19078.41796875 │ 0.5130587815999419 │
│   444   │  19121.484375  │ 0.5127222509215631 │
│   445   │ 19164.55078125 │ 0.5123909944231342 │
│   446   │ 19207.6171875  │ 0.5120649956072845 │
│   447   │ 19250.68359375 │ 0.5117442382634171 │
│   448   │    19293.75    │ 0.5114287064656449 │
│   449   │ 19336.81640625 │ 0.5111183845708782 │
│   450   │ 19379.8828125  │ 0.5108132572168131 │
│   451   │ 19422.94921875 │ 0.510513309320106  │
│   452   │  19466.015625  │ 0.5102185260744513 │
│   453   │ 19509.08203125 │ 0.5099288929488163 │
│   454   │ 19552.1484375  │ 0.5096443956856099 │
│   455   │ 19595.21484375 │ 0.5093650202989676 │
│   456   │  19638.28125   │ 0.5090907530730271 │
│   457   │ 19681.34765625 │ 0.508821580560234  │
│   458   │ 19724.4140625  │ 0.5085574895797701 │
│   459   │ 19767.48046875 │ 0.5082984672158453 │
│   460   │  19810.546875  │ 0.5080445008162383 │
│   461   │ 19853.61328125 │ 0.5077955779906953 │
│   462   │ 19896.6796875  │ 0.5075516866094295 │
│   463   │ 19939.74609375 │ 0.507312814801715  │
│   464   │   19982.8125   │ 0.5070789509543787 │
│   465   │ 20025.87890625 │ 0.5068500837104595 │
│   466   │ 20068.9453125  │ 0.5066262019678154 │
│   467   │ 20112.01171875 │ 0.5064072948778042 │
│   468   │  20155.078125  │ 0.5061933518439725 │
│   469   │ 20198.14453125 │ 0.5059843625208035 │
│   470   │ 20241.2109375  │ 0.5057803168124598 │
│   471   │ 20284.27734375 │ 0.505581204871614  │
│   472   │  20327.34375   │ 0.5053870170982109 │
│   473   │ 20370.41015625 │ 0.5051977441384068 │
│   474   │ 20413.4765625  │ 0.5050133768834073 │
│   475   │ 20456.54296875 │ 0.5048339064683957 │
│   476   │  20499.609375  │ 0.5046593242714833 │
│   477   │ 20542.67578125 │ 0.5044896219126933 │
│   478   │ 20585.7421875  │ 0.5043247912529791 │
│   479   │ 20628.80859375 │ 0.5041648243932378 │
│   480   │   20671.875    │ 0.504009713673408  │
│   481   │ 20714.94140625 │ 0.5038594516715438 │
│   482   │ 20758.0078125  │ 0.5037140312029538 │
│   483   │ 20801.07421875 │ 0.5035734453193609 │
│   484   │  20844.140625  │ 0.503437687308073  │
│   485   │ 20887.20703125 │ 0.5033067506912174 │
│   486   │ 20930.2734375  │ 0.5031806292249383 │
│   487   │ 20973.33984375 │ 0.5030593168987302 │
│   488   │  21016.40625   │ 0.5029428079346752 │
│   489   │ 21059.47265625 │ 0.5028310967868006 │
│   490   │ 21102.5390625  │ 0.5027241781404297 │
│   491   │ 21145.60546875 │ 0.5026220469115427 │
│   492   │  21188.671875  │ 0.5025246982462077 │
│   493   │ 21231.73828125 │ 0.5024321275200101 │
│   494   │ 21274.8046875  │ 0.5023443303374796 │
│   495   │ 21317.87109375 │ 0.5022613025316506 │
│   496   │   21360.9375   │ 0.5021830401634999 │
│   497   │ 21404.00390625 │ 0.5021095395215267 │
│   498   │ 21447.0703125  │ 0.5020407971213457 │
│   499   │ 21490.13671875 │ 0.5019768097052246 │
│   500   │  21533.203125  │ 0.5019175742417712 │
│   501   │ 21576.26953125 │ 0.5018630879255096 │
│   502   │ 21619.3359375  │ 0.5018133481766471 │
│   503   │ 21662.40234375 │ 0.5017683526406201 │
│   504   │  21705.46875   │ 0.501728099188062  │
│   505   │ 21748.53515625 │ 0.5016925859143093 │
│   506   │ 21791.6015625  │ 0.5016618111393562 │
│   507   │ 21834.66796875 │ 0.5016357734075525 │
│   508   │  21877.734375  │ 0.5016144714874873 │
│   509   │ 21920.80078125 │ 0.5015979043718453 │
│   510   │ 21963.8671875  │ 0.5015860712772731 │
│   511   │ 22006.93359375 │ 0.5015789716443024 │
└─────────┴────────────────┴────────────────────┘

RangeError: Maximum call stack size exceeded

If the passed vector size to fft function has length 0. This runs into an infinite loop and gives an error.

RangeError: Maximum call stack size exceeded error
at var X_evens = fft(vector.filter(even)), in fft.js
It is better to add a condition to check this

if (N == 1) {
if (Array.isArray(vector[0])) //If input vector contains complex numbers
return [[vector[0][0], vector[0][1]]];
else
return [[vector[0], 0]];
} ** else If (N <1) //throw or return error**

[1, 1, 1] signal is not working

> console.log(fft([1,1,1]))
TypeError: Cannot read property '0' of undefined
    at Object.complexMultiply [as multiply] (/Users/afanasy/fft/node_modules/fft-js/src/complex.js:24:22)

Input type

Hello guys.

I'm tryng use the lib with an array containing vibration values (mm/s or g).
For example, I have the following input:
[10, 0.8285441, 0.69100095, 1.0226318, 0.7041154, 1.51439095] ...
But when I try to do:

let fft2 = require('fft-js').fft;
let y = fft2(signal);
console.log(y);

Nothing happens (there is no output). I guess that the library isn't working for me.
I found into README.md file the following: "The following code assumes a complex number is
an array: [real, imaginary]", that way, I'll need to get the real and imaginay parts of my original input?

Could you help me?

Thanks and congratulations by the project.

Typing

Hi, thank you for your library
while working with it in a TS project I wrote some types
hope they help someone

declare module "fft-js" {
  type Phasors = [number, number][];

  function dft(vector: number[]): Phasors;
  function fft(vector: number[]): Phasors;
  function fftInPlace(): unknown;
  function idft(phasors: Phasors): Phasors;
  function ifft(phasors: Phasors): Phasors;
  const util: {
    exponent: () => void;
    fftFreq: (phasors: Phasors, freq: number) => unknown;
    fftMag: (phasors: Phasors) => unknown;
  };
}

Can't get phasors from Buffer

I need get frequencies of .wav file.
I can read file via fs

   fs.readFile('output.wav', (err, buffer) => { 
    var phasors= fft(buffer); // exception here
   })

Exception

/node_modules/fft-js/src/complex.js:24
    return [(a[0] * b[0] - a[1] * b[1]),
TypeError: Cannot read property '0' of undefined

Is this possible to get phasors from Buffer? Or exist another way to get phasors from .wav file?

Precision

How to set a precision for output data?

Phase units

What are the phase output units for fft-js? (radians or degrees, or something more exotic?)

Thanks!

Int16Array

Hi

I recieve data from RTCAudioSink node webrtc (wrtc) nonstandard object like this

image

Tell me please how to properly pass this data to fft.fft() function?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.