basic (level) convolution

I was reading today Steven Smith's DSP Guidechapter 6/3

and translated the Basic code
100  'CONVOLUTION USING THE INPUT SIDE ALGORITHM 
110                            ' 
120 DIM X[80]             'The input signal, 81 points 
130 DIM H[30]             'The impulse response, 31 points 
140 DIM Y[110]           'The output signal, 111 points 
150                            ' 
160 GOSUB XXXX  'Mythical subroutine to load X[ ] and H[ ] 
170                            ' 
180 FOR I% = 0 TO 110 'Zero the output array 
190   Y(I%) = 0 
200 NEXT I% 
210                            ' 
220 FOR I% = 0 TO 80 'Loop for each point in X[ ] 
230   FOR J% = 0 TO 30 
       'Loop for each point in H[ ] 
240     Y[I%+J%] = Y[I%+J%] + X[I%]tH[J%] 
250   NEXT J% 
260 NEXT I% '(remember, t is multiplication in programs!) 
270                             ' 
280 GOSUB XXXX    'Mythical subroutine to store Y[ ] 
290                 ' 
300 END 

into Lisp (S7 Scheme)



> (define (convolve x h) ;x for input signal, h for inpulse response;
  (loop with y = (make-list (- (+ (length x) (length h)) 1) 0)
        for i below (length x)
        do
        (loop for xh from i below (+ i (length h))
              for ih from 0 below (length h)
              do
              (list-set! y xh (+ (list-ref y xh) (* (list-ref x i)
                                                    (list-ref h ih)))))
        finally (return y)))



> (convolve  '(1.1 -2.7 -1.8 -2.1 2.9 -1.7 1.3 0.0 -2.9 -0.1 0.2) '(1.3 -1 -3))

(1.43 -4.61 -2.94 7.17 11.27 1.19 -5.31 3.8 -7.67 2.77 9.06 0.1 -0.6)




Comments

Popular Posts