forked from corbanbrook/dsp.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a simple benchmarking suite. Run with 'make benchmark'
- Loading branch information
1 parent
864bad1
commit e361d92
Showing
5 changed files
with
87 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Make sure $JSSHELL points to your js shell binary in .profile or .bashrc | ||
# Most targets use commands that need a js shell path specified | ||
JSSHELL ?= $(error Specify a valid path to a js shell binary in ~/.profile: export JSSHELL=C:\path\js.exe or /path/js) | ||
|
||
# Version number used in naming release files. | ||
VERSION ?= $(error Specify a version for your release (e.g., VERSION=0.5)) | ||
FREQUENCY ?= 440 | ||
|
||
benchmark: | ||
${JSSHELL} -j -e 'var FREQUENCY=${FREQUENCY};' -f dsp.js -f ./bench/bench.js -f ./bench/dft.js | ||
${JSSHELL} -j -e 'var FREQUENCY=${FREQUENCY};' -f dsp.js -f ./bench/bench.js -f ./bench/fft.js | ||
${JSSHELL} -j -e 'var FREQUENCY=${FREQUENCY};' -f dsp.js -f ./bench/bench.js -f ./bench/rfft.js | ||
|
||
clean: | ||
rm -fr ./release |
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,12 @@ | ||
function benchmark(func, loopCount) { | ||
loopCount = loopCount || 10000; | ||
|
||
var start = Date.now(); | ||
|
||
for (var i = 0; i < loopCount; i++) { | ||
func(); | ||
} | ||
|
||
var end = Date.now(); | ||
return end - start; | ||
} |
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,20 @@ | ||
var bufferSize = 2048; | ||
var sampleRate = 44100; | ||
var frequency = FREQUENCY || 440; | ||
|
||
var dft = new DFT(bufferSize, sampleRate); | ||
var osc = new Oscillator(DSP.SAW, frequency, 1.0, bufferSize, sampleRate); | ||
var signal = osc.generate(); | ||
|
||
var duration = benchmark(function() { dft.forward(signal); }, 20); | ||
|
||
var peakBand = 0; | ||
|
||
for (var i = 0; i < dft.spectrum.length; i++) { | ||
peakBand = (dft.spectrum[i] > dft.spectrum[peakBand]) ? i : peakBand; | ||
} | ||
|
||
var peakFreq = dft.getBandFrequency(dft.peakBand); | ||
|
||
print("Detected peak: " + peakFreq + " Hz (error " + Math.abs(peakFreq - frequency) + " Hz)"); | ||
print("20 DFTs: " + (duration) + " ms (" + ((duration) / 20) + "ms per DFT)\n"); |
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,20 @@ | ||
var bufferSize = 2048; | ||
var sampleRate = 44100; | ||
var frequency = FREQUENCY || 440; | ||
|
||
var fft = new FFT(bufferSize, sampleRate); | ||
var osc = new Oscillator(DSP.SAW, frequency, 1.0, bufferSize, sampleRate); | ||
var signal = osc.generate(); | ||
|
||
var duration = benchmark(function() { fft.forward(signal); }); | ||
|
||
var peakBand = 0; | ||
|
||
for (var i = 0; i < fft.spectrum.length; i++) { | ||
peakBand = (fft.spectrum[i] > fft.spectrum[peakBand]) ? i : peakBand; | ||
} | ||
|
||
var peakFreq = fft.getBandFrequency(fft.peakBand); | ||
|
||
print("Detected peak: " + peakFreq + " Hz (error " + Math.abs(peakFreq - frequency) + " Hz)"); | ||
print("10000 FFTs: " + (duration) + " ms (" + ((duration) / 10000) + "ms per FFT)\n"); |
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,20 @@ | ||
var bufferSize = 2048; | ||
var sampleRate = 44100; | ||
var frequency = FREQUENCY || 440; | ||
|
||
var fft = new RFFT(bufferSize, sampleRate); | ||
var osc = new Oscillator(DSP.SAW, frequency, 1.0, bufferSize, sampleRate); | ||
var signal = osc.generate(); | ||
|
||
var duration = benchmark(function() { fft.forward(signal); }); | ||
|
||
var peakBand = 0; | ||
|
||
for (var i = 0; i < fft.spectrum.length; i++) { | ||
peakBand = (fft.spectrum[i] > fft.spectrum[peakBand]) ? i : peakBand; | ||
} | ||
|
||
var peakFreq = fft.getBandFrequency(fft.peakBand); | ||
|
||
print("Detected peak: " + peakFreq + " Hz (error " + Math.abs(peakFreq - frequency) + " Hz)"); | ||
print("10000 FFTs: " + (duration) + " ms (" + ((duration) / 10000) + "ms per FFT)\n"); |