(defun hilbert (x)"Compute analytic signal of real signal vector X using Hilbert transform."(let*((xx (napa-fft:fft (zeropad-pow2 x)))(yy (make-array(length xx):initial-element #C(00)))(zz nil)(out (make-array(length x))))(setf(aref yy 0)(aref xx 0))(loop for n from 1 below (/(length xx)2)do(setf(aref yy n)(*2(aref xx n))))(setf(aref yy (/(length xx)2))(aref xx (/(length xx)2)))(loop for n from (1+(/(length xx)2)) below (length xx)do(setf(aref yy n)0))(setq zz (napa-fft:ifft yy))(loop for n from 0 below (length out)do(setf(aref out n)(aref zz n)))
out))
(defun sample-time (dur &optional(fs +samprate+))"Make a vector of time values of each sample in seconds for specified duration."(coerce(mapcar #'(lambda(x)(/ x fs))(range (round(* dur fs))))'vector))(defun range (n)"Generates a list of integers from 0 to N-1."(linspace 0(1- n) n))(defun linspace (x1 x2 &optional(n 100))"Generates a list of N (100 by default) equally spaced numbers between X1 and X2."(let((x (loop for x from 0 to (1- n)
collect (/ x (1- n)))))(mapcar #'(lambda(v)(+ v x1))(mapcar #'(lambda(v)(* v (- x2 x1)))
x))))
(defun normalize (x &optional(dB -1.0))"Makes a sampled signal normalized to specified level (in dB)."(let((res (make-array(array-dimensions x):initial-element0.0))(gain (db->amp dB))(tmpmax (loop for n from 0 to (1-(length x))
maximize (abs(aref x n)))))(dotimes(n (length x))(setf(aref res n)(* gain (/(aref x n) tmpmax))))
res))(defun db->amp (dB &optional(p0 1.0))(*(expt10(/ dB 20)) p0))