# DSP_FIRgain test.bb

Post Reply
User avatar
Dutchman
Posts: 148
Joined: Tue Aug 06, 2019 4:47 pm
Location: Netherlands

# DSP_FIRgain test.bb

Post by Dutchman » Wed Nov 20, 2019 1:03 pm

# DSP_FIRgain test.bb is a test and demo program for "! DSP utils"
Detailed info on the algorithms can be found in the document "Linear Phase FIR filters.pdf" which is attached
Linear Phase FIR filters.pdf
(444.25 KiB) Downloaded 5 times

Code: Select all

'DSP_FIRgain TEST, frequency-gain characteristic
'by Ton Nillesen, 2019
'See http://eeweb.poly.edu/iselesni/EL713/zoom/linphase.pdf
'
'==== Prepare canvas
{/Extensions/Math/! DSP utils}
{/Extensions/Interface/! Int_}
DRAW MANUAL
NextButt=Int_.Bnext(0) ' assign Id
Caption=Int_.Scapt(0) ' assign Id
'--- activate  caption
CALL Int_Sign(Int_.Scapt,Int_.ScaptCol)
DRAW Update
'
'==== Settings
Test=0 ' show characteristics
f1=0   ' start frequency, relative to Fsample
f2=0.5 ' stop frequency, relative to Fsample

'==== Constants ====
SCRSIZE 600 400
sw sh = SCRSIZE|2
rb gb bb=0.8,1,0.8 ' background color
DRAW SIZE 1
radius=3
margin=50 ' left and right margin of plot
x0=margin ' start-x
y0=sh-2*margin ' baseline
ymax=0.9*y0 ' maximum y
'
'==== Examples of FIRtypes 1 to 4 ====
DIM h1()=(-0.047 ,0.101 ,-0.151 ,0.187 ,1.000 ,0.187 ,-0.151 ,0.101 ,-0.047)
DIM h2()=(0.042 ,-0.009 ,-0.050 ,0.053 ,0.034 ,-0.127 ,0.060 ,0.519 ,0.519 ,          0.060 ,-0.127 ,0.034 ,0.053 ,-0.050 ,-0.009 ,0.042 )
DIM h3()=(-0.027,0.055,-0.089,0.133,-0.200,0.322,-0.670,0,0.670,-0.322,0.200,-0.133,0.089,-0.055,0.027)
DIM h4()=(-0.1,0.6,-0.6,0.1)
'
'==== Main ====
DRAW COLOR 1,0,0
'--- show all types
Type=1
DO IF Type<5
  DRAW CLEAR rb,gb,bb
  INSIGNTEXT caption  "FIR type "+STRTEXT type
  DIM H()
  IF type=1 THEN H=h1
  IF type=2 THEN H=h2
  IF type=3 THEN H=h3
  IF type=4 THEN H=h4
  DIM A()
  A Amax Amin = DSP_FIRgain|3(H,f1,f2,(f2-f1)/(sw-2*margin))  
  n1=(SIZEOF a)-1
  GOSUB Rectify
  IF Test THEN GOSUB Chars
  GOSUB Plot
  DRAW UPDATE
  WaitForNext
  Type+=1
REDO
'--- finish
INBUTT DELETE NextButt
INSIGN TEXT Caption "Done"
END
'
'==== Functions and subroutines
Plot:
DRAW CLEAR rb,gb,bb
dx=(sw-2*margin)/n1
dy=ymax/Amax
'--- axes
DRAW COLOR 0,0,0
DRAW SIZE 2
DRAW LINE x0,y0,x0+n1*dx,y0
DRAW LINE x0,y0,x0,0
'--- graduation
DRAW FONT "Menlo",16
th=STRHEIGHT "X"
'--- x-marks
x1=FLOOR(10*f1)/10
x2=CEIL(10*f2)/10
FOR i=x1 TO x2 STEP 0.1
  txt$=(STRTEXT i "0.0")
  tw=STRWIDTH txt$
  x=(i-x1)*n1/(f2-f1)
  DRAW TEXT txt$,x0+x-tw/2, y0+2*th
  DRAW LINE x0+x,y0,x0+x,y0+th/2
  DRAW SIZE 1
  DRAW LINE x0+x,y0,x0+x,0
NEXT i
'--- y-marks
DRAW SIZE 2
atop=CEIL(10*Amax)/10
FOR i=0 TO atop STEP 0.2
  txt$=(STRTEXT i "0.0")
  tw=STRWIDTH txt$
  y=y0-i*dy
  DRAW TEXT txt$,x0-1.5*tw, y+th/2
  DRAW LINE x0,y,x0-th/2,y
  DRAW SIZE 1
  DRAW LINE x0,y,x0+n1*dx,y
NEXT i
'--- curve
DRAW COLOR 1,0,0
DRAW SIZE 3
DRAW AT x0,y0-dy*A(0)
FOR i=0 TO n1
  x=x0+i*dx : y=y0-dy*A(i)
  DRAW TO x,y
NEXT i
RETURN
'
Rectify: ' make positive and set max value
FOR i=0 TO n1 : A(i)=ABS(A(i)) : NEXT i
IF n1<25 THEN 'print list
  PRINT "List A size =";n1+1
  FOR i=0 TO n1
    PRINT A(i)
  NEXT i
ENDIF
Amax=MAX(Amax,ABS(Amin)) ' rectified
Amin=0
RETURN
'
Chars: ' print characteristics
  PRINT "FIR type="+(STRTEXT DSP_FIRgain.type "?, ");
  PRINT " M="+(STRTEXT DSP_FIRgain.m "?, ");
  PRINT " Size="+(STRTEXT DSP_FIRgain.n "? coëfficients")
  PRINT "Error="+(STRTEXT ERROR)
  PRINT "Frequency-gain list A =";n1+1;"points"
  PRINT "Amax =";Amax : PRINT "Amin =";Amin
  PRINT
RETURN
Screenshot:
'
FIRgain.JPG
FIRgain.JPG (40.94 KiB) Viewed 111 times
It is still a long way to go

Post Reply