Skip to content

Commit

Permalink
feat(dsp): add/update power & integral fns
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Dec 18, 2020
1 parent d51a17c commit f455fad
Showing 1 changed file with 53 additions and 7 deletions.
60 changes: 53 additions & 7 deletions packages/dsp/src/fft/power.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,78 @@
import type { NumericArray } from "@thi.ng/api";
import { isNumber } from "@thi.ng/checks";
import type { ComplexArray } from "../api";
import { isComplex } from "../util/complex";

/**
* Computes the `sum(f(i)^2)` for given array.
* Computes the sum of the given array.
*
* @param window
*/
export const integralT = (window: NumericArray) => {
let sum = 0;
for (let i = window.length; --i >= 0; ) {
// prettier-ignore
sum += (<number>window[i]) ** 2;
sum += window[i];
}
return sum;
};

/**
* Computes the `sum(|c(i)|^2)` for given complex array.
* Computes the squared sum of given array.
*
* @param window
*/
export const integralTSquared = (window: NumericArray) => {
let sum = 0;
for (let i = window.length; --i >= 0; ) {
sum += window[i] ** 2;
}
return sum;
};

/**
* Computes the `sum(|c(i)|)` for given complex array.
*
* @param window
*/
export const integralF = ([real, img]: ComplexArray) => {
let sum = 0;
for (let i = real.length; --i >= 0; ) {
sum += Math.hypot(real[i], img[i]);
}
return sum;
};

/**
* Computes the `sum(|c(i)|^2)` for given complex array.
*
* @param window
*/
export const integralFSquared = ([real, img]: ComplexArray) => {
let sum = 0;
for (let i = real.length; --i >= 0; ) {
sum += real[i] ** 2 + img[i] ** 2;
}
return sum;
};

/**
* If `scale` is a number, returns it. Else returns `base / integralT(scale)`.
*
* @param scale
* @param base
*/
export const powerScale = (scale: number | NumericArray, base = 1) =>
isNumber(scale) ? scale : base / integralT(scale);

/**
* If `scale` is a number, returns it. Else returns `integralT(scale) / base`.
*
* @param scale
* @param base
*/
export const invPowerScale = (scale: number | NumericArray, base = 1) =>
isNumber(scale) ? scale : integralT(scale) / base;

/**
* Computes sum squared power of given time or frequency domain window.
*
Expand All @@ -41,8 +85,8 @@ export const integralF = ([real, img]: ComplexArray) => {
*/
export const powerSumSquared = (window: NumericArray | ComplexArray) =>
isComplex(window)
? integralF(window) / window[0].length
: integralT(window);
? integralFSquared(window) / window[0].length
: integralTSquared(window);

/**
* Computes mean squared power of given time or frequency domain window.
Expand Down Expand Up @@ -72,4 +116,6 @@ export const powerMeanSquared = (window: NumericArray | ComplexArray) =>
export const powerTimeIntegral = (
window: NumericArray | ComplexArray,
fs: number
) => (isComplex(window) ? integralF(window) : integralT(window)) / fs;
) =>
(isComplex(window) ? integralFSquared(window) : integralTSquared(window)) /
fs;

0 comments on commit f455fad

Please sign in to comment.