3

I have an array of N measurements that I should present as a graph, but the graph can be only of M pixels wide and is scrolled only by M pixels.

While M is constant, N can be anything from tens to thousands. Each time when I need to show the graph, I know what N is, however as N/M can be not integer, there is an accumulated error that I want somehow to compensate.

I am working in a plain C, and no math libraries can be used.

EDIT 2: The data is relatively homogeneous with peaks once in a while, and I do not want to miss these peaks, while interpolating.

EDIT 3: I am looking for solution that will work good enough for any N, greater than M and lesser than M.

Thanks.

Flot2011
  • 4,601
  • 3
  • 44
  • 61
  • 2
    http://en.wikipedia.org/wiki/Interpolation – Ignacio Vazquez-Abrams May 20 '12 at 15:10
  • No, it is an embedded device with a very strict memory constrains:-) – Flot2011 May 20 '12 at 15:12
  • 1
    @IgnacioVazquez-Abrams: "libm" is part of "plain C" is one means a hosted implementation. Putting it in a separate library file on unix systems is an ugly historical artifact, nothing more. On the other hand, if this embedded system only has a freestanding implementation, essentially the whole standard library is missing. – R.. GitHub STOP HELPING ICE May 20 '12 at 15:15

1 Answers1

4

One good solution is not to iterate over your input samples, but over your output positions. That is, you will always draw exactly M pixels. To calculate the nearest sample value for the ith pixel, use the array offset:

[(i*N+M/2)/M]

Of course only using the nearest sample will give a very aliased result (discarding most of your samples in the case where N is large). If you're sure N will always be larger than M, a good but simple approach is to average sufficient neighboring samples with a weighted average such that each sample gets a total weight of 1 (with endpoints having their weight split between neighboring output pixels). Of course there are more elaborate resampling algorithms you can use that may be more appropriate (especially if your data is something like audio samples which are more meaningful in the frequency domain), but for an embedded device with tight memory and clock cycle requirements, averaging is likely the approach you want.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • N may be times lesser and times greater that M, but thanks to your answer I have started to think that probably the best way is to use 2 different methods for these 2 cases. – Flot2011 May 20 '12 at 15:31
  • Yes, you definitely need different methods for both cases. When `N` is smaller, linear interpolation between the two nearest samples is the best simple solution you'll get. When `N` is larger, averaging is the best simple solution. – R.. GitHub STOP HELPING ICE May 20 '12 at 15:42