Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding magnitude and frequency utility functions.
- Loading branch information
jjung
committed
Jun 24, 2015
1 parent
20dcd78
commit 71371a5
Showing
7 changed files
with
161 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
//------------------------------------------------- | ||
// Add two complex numbers | ||
//------------------------------------------------- | ||
var complexAdd = function (a, b) | ||
{ | ||
return [a[0] + b[0], a[1] + b[1]]; | ||
}; | ||
|
||
//------------------------------------------------- | ||
// Subtract two complex numbers | ||
//------------------------------------------------- | ||
var complexSubtract = function (a, b) | ||
{ | ||
return [a[0] - b[0], a[1] - b[1]]; | ||
}; | ||
|
||
//------------------------------------------------- | ||
// Multiply two complex numbers | ||
// | ||
// (a + bi) * (c + di) = (ac - bd) + (ad + bc)i | ||
//------------------------------------------------- | ||
var complexMultiply = function (a, b) | ||
{ | ||
return [(a[0] * b[0] - a[1] * b[1]), | ||
(a[0] * b[1] + a[1] * b[0])]; | ||
}; | ||
|
||
//------------------------------------------------- | ||
// Calculate |a + bi| | ||
// | ||
// sqrt(a*a + b*b) | ||
//------------------------------------------------- | ||
var complexMagnitude = function (c) | ||
{ | ||
return Math.sqrt(c[0]*c[0] + c[1]*c[1]); | ||
}; | ||
|
||
//------------------------------------------------- | ||
// Exports | ||
//------------------------------------------------- | ||
module.exports = { | ||
add: complexAdd, | ||
subtract: complexSubtract, | ||
multiply: complexMultiply, | ||
magnitude: complexMagnitude | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/*===========================================================================*\ | ||
* Fast Fourier Transform Frequency/Magnitude passes | ||
* | ||
* (c) Vail Systems. Joshua Jung and Ben Bryan. 2015 | ||
* | ||
* This code is not designed to be highly optimized but as an educational | ||
* tool to understand the Fast Fourier Transform. | ||
\*===========================================================================*/ | ||
|
||
//------------------------------------------------- | ||
// The following code assumes a complex number is | ||
// an array: [real, imaginary] | ||
//------------------------------------------------- | ||
var complex = require('./complex'); | ||
|
||
//------------------------------------------------- | ||
// Calculate FFT Magnitude for complex numbers. | ||
//------------------------------------------------- | ||
var fftMag = function (fftBins) { | ||
return fftBins.map(complex.magnitude); | ||
}; | ||
|
||
//------------------------------------------------- | ||
// Calculate Frequency Bins | ||
// | ||
// Returns an array of the frequencies (in hertz) of | ||
// each FFT bin provided, assuming the sampleRate is | ||
// samples taken per second. | ||
//------------------------------------------------- | ||
var fftFreq = function (fftBins, sampleRate) { | ||
var stepFreq = sampleRate / (fftBins.length * 2); | ||
|
||
return fftBins.map(function (__, ix) { | ||
return ix * stepFreq; | ||
}); | ||
}; | ||
|
||
//------------------------------------------------- | ||
// Exports | ||
//------------------------------------------------- | ||
module.exports = { | ||
fftMag: fftMag, | ||
fftFreq: fftFreq | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters