## ! DSP utils

Dutchman
Posts: 148
Joined: Tue Aug 06, 2019 4:47 pm
Location: Netherlands

### ! DSP utils

! DSP utils {/Extensions/Math/! DSP utils}
Contains functions for Digital Signal Processing

Code: Select all

``````' {/Extensions/Math/! DSP utils}
[ Functions for Digital Signal Processing ]
'
[–––––––––––––––––––––––– Functions ––––––––––––––––––––––––
DSP_FIRgain(h(),f1,f2,df)
Frequency-gain characteristic of linear phase FIR filter
h(0)=filter coefficients, f1 to f2=freq range, df=freq step
See http://eeweb.poly.edu/iselesni/EL713/zoom/linphase.pdf
Error=2 if size of h()<2
Error=4 if h() is not symmetric or anti-symmetric
Error=5 if parameters are not accepted
Function returns: List with gain data, max gain, min gain
–––
End of function description –––––––––––]
'
[–––––––––––––––––––––– Implementation ––––––––––––––––––––––]
DEF DSP_FIRgain(h(),f1,f2,df) 'by Ton Nillesen, October 2019
N=SIZE OF h()
DIM A() ' frequency gain characteristic
IF N<2 : ERROR=10 : RETURN A() : ENDIF
IF (df<=0) OR (f2-f1)<=0 : ERROR=12 : RETURN A() : ENDIF
M=(N-1)/2
'--- check symmetry
FOR i=0 TO m
IF ABS(h(i))<>ABS(h(n-1-i)) : ERROR=11 : RETURN A()
ENDIF
NEXT i
'--- set type
GOSUB SetOdd
IF Odd
IF h(0)=h(n-1) THEN Type=1 ELSE Type=3
ELSE : IF h(0)=h(n-1) THEN Type=2 ELSE Type=4
ENDIF
'---- Presets
Amax=0 : Amin=0
IF (Type=1) or (Type=3) : imax=M-1
ELSE : imax=N/2-1
ENDIF
'--- process
FOR f=f1 TO f2 STEP df
A+0 : j=(SIZE OF A)-1 ' new entrie A(j)
IF type=1 THEN A(j)+=h(M) ' A=H(m) at f=0
IF Type<3 THEN GOSUB Cosine ELSE GOSUB Sine
NEXT f
RETURN A() Amax Amin ' from function
SetOdd: '---  local subroutine
IF MATHEVEN(n) THEN Odd=0 ELSE Odd=1
RETURN ' from subroutine
Cosine: '--- local subroutine
w=2PI*f ' angular frequency
FOR i=0 TO imax
A(j)+=2*h(i)*COS((M-i)*w)
Amax=MAX(Amax,A(j)) : Amin=MIN(Amin,A(j))
NEXT i
RETURN
Sine: '--- local subroutine
w=2PI*f ' angular frequency
FOR i=0 TO imax
A(j)+=2*h(i)*SIN((M-i)*w)
Amax=MAX(Amax,A(j)) : Amin=MIN(Amin,A(j))
NEXT i
RETURN
END DEF``````
# DSP_FIRgain test.bb is a test and demo program