import type { FnU3 , NumericArray } from "@thi.ng/api" ; import { cossin } from "@thi.ng/math/angle" ; import { TAU } from "@thi.ng/math/api" ; import type { FilterConfig , FilterResponse } from "./api.js" ; import { magDb } from "./convert.js" ; import { line } from "./line.js" ; param zeroes param poles param freq param db export const filterResponseRaw = ( zeroes , poles , freq , db = true ) : FilterResponse => { const w0 = TAU * freq ; const [ cp , sp ] = convolve ( poles , w0 ) ; const [ cz , sz ] = convolve ( zeroes , w0 ) ; const mag = Math . sqrt ( ( cz * cz + sz * sz ) / ( cp * cp + sp * sp ) ) ; const phase = Math . atan2 ( sp , cp ) - Math . atan2 ( sz , cz ) ; return { freq , phase , mag : db ? magDb ( mag ) : mag } ; } ; export const filterResponse = ( coeffs , freq , db ? ) => filterResponseRaw ( coeffs . zeroes , coeffs . poles , freq , db ) ; export const freqRange = ( fstart , fend , num ) => line ( fstart , fend , num - 1 ) . take ( num ) ; const convolve = ( coeffs , w0 ) => { let c = 0 ; let s = 0 ; for ( let i = coeffs . length ; i -- > 0 ; ) { const k = cossin ( w0 * i , coeffs [ i ] ) ; c += k [ 0 ] ; s += k [ 1 ] ; } return [ c , s ] ; } ;