# Odd-Line Smoothing of Array

QUESTION: I am trying to write an IDL program for "smoothing" over lines of image data acquired with an aerial CCD system. This requires reading the odd lines, calculating the mean and placing it in the even. The images are a constant 640x480 pixels. For example, the following lines:

```   line 1 :   2  2  2  2...
line 2 :   x  x  x  x...
line 3 :   4  4  4  4...
```

Turn into this, after processing:

```   line 1 :   2  2  2  2...
line 2 :   3  3  3  3...
line 3 :   4  4  4  4...
```

I can think of several ways to implement this but I am looking for the most efficient.

Here is one solution, although with a caveat: the last row in the image is not handled properly, so you will have to come up with a way to deal with that. But here's what I would probably do. (I'm sure JD, David, or someone else can probably come up with something a bit more clever.)
```   ; picks off the odd rows
oddlines = oldimage[*, indgen(240) * 2]

; averages odd row with odd row below it by adding the odd lines
; to themselves shifted up a row
evenlines = (oddlines + shift(oddlines, -640)) * .5

; puts the even rows after the odd rows
; and then reshapes so they are below
new_image = reform([oddlines, evenlines], 640, 480)
```

The Shift statement moves row 3 to row 1, and row 1 to row 479, that means in the final image, row 480 is the mean of row 1 and row 479, which is probably not how you want it.

Other than the last row, I think this works, although it doesn't use Histogram. :-)

Well, I'm not clever enough, but JD certainly is. He suggests a slightly different approach.

This is a good chance to use the mostly neglected stride operator for IDL's range subscripts, which has the syntax [low:high:stride].

```    d = size(a, /DIMENSIONS)
x = indgen(d[0])
y = findgen((d[1]-1)/2)+.5
a[*,1:d[1]-2:2] = interpolate(a[*,0:*:2], x, y, /GRID)
```

Note that the last line is unchanged for images with an even number of lines (that comes from (d[1]-1)/2).

Since your dimensions never change, you can cache x and y and use them over and over. For this reason as well, it might also be faster to expand out [*,1:d[1]-2:2] etc. into index arrays and cache them, rather than have IDL recompute them for each image. It would be lovely if IDL provided a function to convert a given subscript syntax into an array of indices, but I don't believe it has one.

 Web Coyote's Guide to IDL Programming