Re: The Behavior of CONVOL [message #7318 is a reply to message #7314] |
Fri, 01 November 1996 00:00  |
agraps
Messages: 35 Registered: September 1994
|
Member |
|
|
"Kevin R. Turpie" <turpie@seaeagle.gsfc.nasa.gov> writes:
> I've found the behavior of CONVOL to be a bit confusing. Please
> let me know if I'm missing something, but here are my observations:
I've noticed confusing behavior too.
[...]
> Second, when CENTER is set to 0, CONVOL does a convolution in a
> strict sense *if* the input kernel function, say k(x), is defined
> so that k(x) = 0 for all x < 0. The result is usually shifted to
> the right.
So that's what's going on... I've noticed the right shift. I've had to
"fix" (fudge?) the first values to get the result that I knew I should
have gotten.
> To do a true convolution with CONVOL for any kernel, it seems that
> CENTER must be set to 1 and REVERSE must be applied to each dimension
> of the kernel prior to input.
This is really useful, thanks.
I wrote a function a couple of years ago that performed the equivalent
of Matlab's "conv" (convolution) function. (It's crude, but it works.)
That's when I first noticed the different behavior. I'll attach it
below.
> PS - If your interested, I did create a routine to perform
> two dimensional convolutions using a FFT. It is *very* fast
> and behaves like CONVOL with the EDGE_WRAP keyword on and
> the kernel oriented properly.
I'm interested.
Amara
;*********************************************************** ******************
FUNCTION conv, a, b
;+
; NAME:
; CONV
;
; PURPOSE:
; This function performs a convolution operation like Matlab's conv.
;
; CATEGORY:
; Signal Processing
;
; CALLING SEQUENCE:
; y = CONV(a, b)
;
; INPUTS:
; a: 1st vector.
; b: 2nd vector, the "kernel"
;
; OUTPUTS:
; y: a convolved with b. See description in Matlab Reference Manual
; for conv.
;
;
; MODIFICATION HISTORY:
; Written by Amara Graps November, 1994.
;-
;Pad a with zeros of size of the kernel
p = N_ELEMENTS(b)
new_a = [a,FLTARR(p-1)]
;Apply IDL's convolution function
out = CONVOL(new_a, b, center = 0)
;Calculate results for 1st p-1 values (another Fudge to equal
;Matlab's conv). i.e. if p=5 then the first p values are:
;out(0) = new_a(0)*b(0)
;out(1) = new_a(0)*b(1) + new_a(1)*b(0)
;out(2) = new_a(0)*b(2) + new_a(1)*b(1) + new_a(2)*b(0)
;out(3) = new_a(0)*b(3) + new_a(1)*b(2) + new_a(2)*b(1) + new_a(3)*b(0)
;out(4) = new_a(0)*b(4) + new_a(1)*b(3) + new_a(2)*b(2) + new_a(3)*b(1) +
; new_a(4)*b(0)
FOR i = 0, p-1 DO BEGIN
out(i) = new_a(0) * b(i)
FOR l = 1, i DO BEGIN
out(i) = out(i) + new_a(l)*b(i-l)
END ;l
END ;i
RETURN, OUT
END ;function conv
;*********************************************************** ************
--
************************************************************ *************
Amara Graps email: agraps@netcom.com
Computational Physics vita: finger agraps@best.com
Multiplex Answers URL: http://www.amara.com/
************************************************************ *************
|
|
|