From f47684add3825a7865ce9094f4e558067c80c20a Mon Sep 17 00:00:00 2001 From: Hugh Rawlinson Date: Mon, 13 Jan 2020 22:11:18 -0500 Subject: [PATCH] WIP --- src/extractors/autocorrelation.js | 33 ++- test/extractors/autocorrelation.js | 13 + .../autocorrelationTestData/sin128.ac.json | 257 ++++++++++++++++++ .../autocorrelationTestData/sin128.json | 130 +++++++++ 4 files changed, 422 insertions(+), 11 deletions(-) create mode 100644 test/extractors/autocorrelationTestData/sin128.ac.json create mode 100644 test/extractors/autocorrelationTestData/sin128.json diff --git a/src/extractors/autocorrelation.js b/src/extractors/autocorrelation.js index 44438e27..14730387 100644 --- a/src/extractors/autocorrelation.js +++ b/src/extractors/autocorrelation.js @@ -1,3 +1,21 @@ +function zeroIfOutOfBound(i, f) { + if (i < 0) { + return 0; + } + if (i >= f.length - 1) { + return 0; + } + return f[i]; +} + +function phi(t, f) { + var acc = 0; + for (var i = 0; i < f.length; i++) { + acc += f[i] * zeroIfOutOfBound(i + t, f); + } + return acc; +} + export default function autocorrelation() { if (typeof arguments[0].signal !== 'object') { throw new TypeError(); @@ -5,16 +23,9 @@ export default function autocorrelation() { const arr = arguments[0].signal; - var ac = new Float32Array(arr.length); - for (var lag = 0; lag < arr.length; lag++) { - var value = 0; - for (var index = 0; index < arr.length - lag; index++) { - let a = arr[index]; - let otherindex = index - lag; - let b = otherindex >= 0 ? arr[otherindex] : 0; - value = value + a * b; - } - ac[lag] = value; + var ac = new Float32Array(arr.length*2); + for (var lag = -1*arr.length; lag < arr.length; lag++) { + ac[lag + arr.length] = phi(lag, arr); } return ac; -} +}; diff --git a/test/extractors/autocorrelation.js b/test/extractors/autocorrelation.js index 9bf4114c..b3a76f8c 100644 --- a/test/extractors/autocorrelation.js +++ b/test/extractors/autocorrelation.js @@ -4,8 +4,21 @@ var TestData = require('../TestData'); // Setup var autocorrelation = require('../../dist/node/extractors/autocorrelation'); +var sin128 = require('./autocorrelationTestData/sin128.json'); +var sin128AC = require('./autocorrelationTestData/sin128.ac.json'); describe('autocorrelation', function () { + it.only('should blah', function(done) { + // assert.deepEqual(autocorrelation({signal: sin128}), new Float32Array(sin128AC)); + autocorrelation({signal:sin128}).forEach((element, index) => { + try { + assert.approximately(element, sin128AC[index], 5); + } catch (e) { + throw Error(`index: ${index}: ${element}, ${sin128AC[index]} \n${e.message}`); + } + }); + }); + it('should return correct autocorrelation value', function (done) { var en = autocorrelation({ signal:TestData.VALID_SIGNAL, diff --git a/test/extractors/autocorrelationTestData/sin128.ac.json b/test/extractors/autocorrelationTestData/sin128.ac.json new file mode 100644 index 00000000..f8e2ef1e --- /dev/null +++ b/test/extractors/autocorrelationTestData/sin128.ac.json @@ -0,0 +1,257 @@ +[ +-1.42108547152020e-14, +-7.10542735760100e-15, +-0.00244567041421462, +-0.00977071172614785, +-0.0243849284966942, +-0.0486623911059354, +-0.0849297287379613, +-0.135454596689161, +-0.202434360511617, +-0.287985038794869, +-0.394130545544929, +-0.522792272139455, +-0.675779047728387, +-0.854777515710488, +-1.06134296255421, +-1.29689063374909, +-1.56268757007610, +-1.85984499567782, +-2.18931128759575, +-2.55186555452970, +-2.94811185056717, +-3.37847404753721, +-3.84319138746754, +-4.34231473437423, +-4.87570354229648, +-5.44302355411090, +-6.04374524323028, +-6.67714300781490, +-7.34229512461187, +-8.03808446699316, +-8.76319998919854, +-9.51613897620849, +-10.2952100560873, +-11.0985369690516, +-11.9240630849471, +-12.7695566582587, +-13.6326168072514, +-14.5106802013424, +-15.4010284383533, +-16.3007960908852, +-17.2069793987153, +-18.1164455818314, +-19.0259427465115, +-19.9321103547260, +-20.8314902250954, +-21.7205380316877, +-22.5956352650847, +-23.4531016184022, +-24.2892077593126, +-25.1001884475991, +-25.8822559563738, +-26.6316137538198, +-27.3444704011781, +-28.0170536216936, +-28.6456244943677, +-29.2264917256394, +-29.7560259515351, +-30.2306740223939, +-30.6469732219889, +-31.0015653727292, +-31.2912107786457, +-31.5128019580274, +-31.6633771178998, +-31.7401333230047, +-31.7404393125664, +-31.6618479188988, +-31.5021080428320, +-31.2591761420008, +-30.9312271892489, +-30.5166650597522, +-30.0141323069534, +-29.4225192890193, +-28.7409726092823, +-27.9689028360000, +-27.1059914687628, +-26.1521971209804, +-25.1077608900986, +-23.9732108895073, +-22.7493659185186, +-21.4373382492873, +-20.0385355121320, +-18.5546616633674, +-16.9877170224825, +-15.3399973682757, +-13.6140920863955, +-11.8128813636002, +-9.93953242696585, +-7.99749482919823, +-5.99049478415788, +-3.92252855966355, +-1.79785493759712, +0.379013245719522, +2.60331846297551, +4.87006874434794, +7.17404878933619, +9.50983191746370, +11.8717928305889, +14.2541211568609, +16.6508357437184, +19.0557996647744, +21.4627359029557, +23.8652436698913, +26.2568153192672, +28.6308538097010, +30.9806906706369, +33.2996044228404, +35.5808394032680, +37.8176249424325, +40.0031948408579, +42.1308070898486, +44.1937637805718, +46.1854311443872, +48.0992596664520, +49.9288042138837, +51.6677441191918, +53.3099031592791, +54.8492693700805, +56.2800146368497, +57.5965140002118, +58.7933646183972, +59.8654043265336, +60.8077297345164, +61.6157138057949, +62.2850228604027, +62.8116329467228, +63.1918455278116, +63.4223024296059, +63.5000000000004, +63.4223024296060, +63.1918455278116, +62.8116329467228, +62.2850228604027, +61.6157138057949, +60.8077297345164, +59.8654043265336, +58.7933646183972, +57.5965140002118, +56.2800146368497, +54.8492693700805, +53.3099031592790, +51.6677441191918, +49.9288042138837, +48.0992596664520, +46.1854311443872, +44.1937637805718, +42.1308070898486, +40.0031948408579, +37.8176249424325, +35.5808394032680, +33.2996044228404, +30.9806906706369, +28.6308538097010, +26.2568153192672, +23.8652436698913, +21.4627359029557, +19.0557996647744, +16.6508357437184, +14.2541211568609, +11.8717928305889, +9.50983191746370, +7.17404878933620, +4.87006874434794, +2.60331846297551, +0.379013245719522, +-1.79785493759712, +-3.92252855966355, +-5.99049478415787, +-7.99749482919823, +-9.93953242696585, +-11.8128813636002, +-13.6140920863955, +-15.3399973682757, +-16.9877170224825, +-18.5546616633674, +-20.0385355121320, +-21.4373382492873, +-22.7493659185186, +-23.9732108895073, +-25.1077608900986, +-26.1521971209804, +-27.1059914687628, +-27.9689028360000, +-28.7409726092823, +-29.4225192890193, +-30.0141323069534, +-30.5166650597522, +-30.9312271892489, +-31.2591761420008, +-31.5021080428320, +-31.6618479188988, +-31.7404393125664, +-31.7401333230047, +-31.6633771178998, +-31.5128019580274, +-31.2912107786457, +-31.0015653727292, +-30.6469732219889, +-30.2306740223939, +-29.7560259515351, +-29.2264917256394, +-28.6456244943677, +-28.0170536216936, +-27.3444704011781, +-26.6316137538198, +-25.8822559563738, +-25.1001884475991, +-24.2892077593126, +-23.4531016184022, +-22.5956352650847, +-21.7205380316877, +-20.8314902250954, +-19.9321103547260, +-19.0259427465115, +-18.1164455818314, +-17.2069793987153, +-16.3007960908852, +-15.4010284383533, +-14.5106802013424, +-13.6326168072514, +-12.7695566582587, +-11.9240630849471, +-11.0985369690516, +-10.2952100560873, +-9.51613897620849, +-8.76319998919854, +-8.03808446699316, +-7.34229512461186, +-6.67714300781490, +-6.04374524323027, +-5.44302355411090, +-4.87570354229649, +-4.34231473437423, +-3.84319138746754, +-3.37847404753720, +-2.94811185056717, +-2.55186555452970, +-2.18931128759576, +-1.85984499567782, +-1.56268757007611, +-1.29689063374910, +-1.06134296255422, +-0.854777515710484, +-0.675779047728383, +-0.522792272139455, +-0.394130545544932, +-0.287985038794872, +-0.202434360511617, +-0.135454596689165, +-0.0849297287379613, +-0.0486623911059407, +-0.0243849284967013, +-0.00977071172615140, +-0.00244567041421462, +-5.32907051820075e-15, +-1.42108547152020e-14 +] diff --git a/test/extractors/autocorrelationTestData/sin128.json b/test/extractors/autocorrelationTestData/sin128.json new file mode 100644 index 00000000..4e735f7d --- /dev/null +++ b/test/extractors/autocorrelationTestData/sin128.json @@ -0,0 +1,130 @@ +[ + 0, + 0.0494537199227389, + 0.0987864183057939, + 0.147877369769468, + 0.196606440529288, + 0.244854382383498, + 0.292503124533408, + 0.339436062522405, + 0.385538343586606, + 0.430697147718822, + 0.474801963758005, + 0.517744859828613, + 0.559420747467949, + 0.599727638795270, + 0.638566896093212, + 0.675843473190815, + 0.711466148057432, + 0.745347746038318, + 0.777405353185625, + 0.807560519162724, + 0.835739449225314, + 0.861873184809571, + 0.885897772285222, + 0.907754419460905, + 0.927389639458407, + 0.944755381604112, + 0.959809149016967, + 0.972514102605548, + 0.982839151219422, + 0.990759027734459, + 0.996254350885672, + 0.999311672696456, + 0.999923511388017, + 0.998088369688553, + 0.993810738497317, + 0.987101085894642, + 0.977975831524801, + 0.966457306414364, + 0.952573698324446, + 0.936358982770554, + 0.917852839878766, + 0.897100557281826, + 0.874152919292751, + 0.849066082627069, + 0.821901438977972, + 0.792725464780426, + 0.761609558532384, + 0.728629866070573, + 0.693867094228944, + 0.657406313335362, + 0.619336749030501, + 0.579751563917634, + 0.538747629577988, + 0.496425289510028, + 0.452888113571918, + 0.408242644528817, + 0.362598137324680, + 0.316066291717458, + 0.268760978930599, + 0.220797962991732, + 0.172294617437928, + 0.123369638083646, + 0.0741427525516275, + 0.0247344272800181, + -0.0247344272799868, + -0.0741427525515963, + -0.123369638083558, + -0.172294617437841, + -0.220797962991701, + -0.268760978930624, + -0.316066291717428, + -0.362598137324651, + -0.408242644528789, + -0.452888113571890, + -0.496425289510001, + -0.538747629577914, + -0.579751563917608, + -0.619336749030521, + -0.657406313335339, + -0.693867094228921, + -0.728629866070551, + -0.761609558532363, + -0.792725464780407, + -0.821901438977954, + -0.849066082627083, + -0.874152919292763, + -0.897100557281837, + -0.917852839878731, + -0.936358982770533, + -0.952573698324436, + -0.966457306414356, + -0.977975831524776, + -0.987101085894646, + -0.993810738497320, + -0.998088369688555, + -0.999923511388016, + -0.999311672696457, + -0.996254350885674, + -0.990759027734460, + -0.982839151219444, + -0.972514102605542, + -0.959809149016960, + -0.944755381604131, + -0.927389639458429, + -0.907754419460918, + -0.885897772285237, + -0.861873184809573, + -0.835739449225378, + -0.807560519162709, + -0.777405353185600, + -0.745347746038358, + -0.711466148057464, + -0.675843473190839, + -0.638566896093225, + -0.599727638795363, + -0.559420747468034, + -0.517744859828591, + -0.474801963757970, + -0.430697147718869, + -0.385538343586642, + -0.339436062522428, + -0.292503124533418, + -0.244854382383604, + -0.196606440529378, + -0.147877369769432, + -0.0987864183058551, + -0.0494537199227853, + -3.13509580581722e-14 +] \ No newline at end of file