Most DSP applications are programmed in C or Fortran. Just the thing you need when programs contain thousands of lines of code, have multiple programmers working together, or will need to be modified and maintained in the future.
However, learning DSP has different requirements than using DSP. You need to concentrate on understanding the algorithms and techniques, without being distracted by the quirks of a particular language. You don't care about power and flexibility; you need simplicity! The programs in this book are written to teach DSP strategies in the most straightforward way, with all other factors being treated as secondary. Good programming style is disregarded if it makes the program logic more clear. For instance:
- a simplified version of BASIC is used
- line numbers are included
- the only control structure allowed is the FOR-NEXT loop
- there are no I/O statements
- integer variables are always identified by a "%" (such as I% or K%)
Here is an example program, showing how to carry out a recursive filter.
100 'RECURSIVE FILTER 110 'INITIALIZE AND DEFINE THE ARRAYS TO BE USED 120 DIM X[499] 'holds the input signal 130 DIM Y[499] 'holds the filtered output signal 140 ' 150 A0 = 0.93 'Set the recursion coefficients 160 A1 = -0.93 '(these values define a high-pass filter) 170 B1 = 0.86 180 ' 190 GOSUB XXXX 'Subroutine to load the input signal into X[ ] 200 ' 210 'FILTER THE SIGNAL BY RECURSION TO FIND Y[ ] 220 FOR I% = 2 TO 499 230 Y[I%] = A0*X[I%] + A1*X[I%-1] + B1*Y[I%-1] 240 NEXT I% 250 ' 260 END
A wide variety of filter responses can be created by simply changing the values of A0, A1, and B1. For instance, the program as written is a high pass filter. It can be changed into a low-pass filter by defining the coefficients to be: A0 = 0.05, A1 = 0, B1 = 0.95. The graphs below show examples of this program in action.
Here is another example program: the Discrete Fourier Transform, one of the most important algorithms in DSP.
100 'THE DISCRETE FOURIER TRANSFORM 110 'The frequency domain signals, held in REX[ ] and IMX[ ], are 120 'calculated from the time domain signal, held in XX[ ]. 130 ' 140 ' 'INITIALIZE AND DEFINE THE ARRAYS TO BE USED 150 DIM XX[511] 'holds the time domain signal 160 DIM REX[256] 'holds the real part of the frequency domain 170 DIM IMX[256] 'holds the imaginary part of the frequency domain 180 ' 190 PI = 3.14159265 'Set the constant, PI 200 N% = 512 'N% is the number of points in XX[ ] 210 ' 220 GOSUB XXXX 'Mythical subroutine that loads data into XX[ ] 230 ' 240 FOR K% = 0 TO 256 'Zero the values in REX[ ] & IMX[ ] so 250 REX[K%] = 0 'they can be used as accumulators 260 IMX[K%] = 0 270 NEXT K% 280 ' 'CALCULATE EACH SAMPLE IN REX[ ] and IMX[ ] 290 ' 300 FOR K% = 0 TO 256 'Loop through each sample in REX[ ] and IMX[ ] 310 FOR I% = 0 TO 511 'Loop through each sample in XX[ ] 320 ' 330 REX[K%] = REX[K%] + XX[I%] * COS(2*PI*K%*I%/N%) 340 IMX[K%] = IMX[K%] - XX[I%] * SIN(2*PI*K%*I%/N%) 350 ' 360 NEXT I% 370 NEXT K% 380 ' 390 END
Don't understand what is happening in lines 330 and 340? Don't worry, three pages in the text are devoted to explaining how this algorithm works. These programs may not be elegant or efficient- that's not their purpose- but they are guaranteed to teach you DSP!