How to reconfigure an array of n elements to an array of m elements

I have an array of N dimensions, which I have to present in the form of a graph, but the graph can only be M pixels wide and scrolls only M pixels.

As long as M is constant, N can be anything from tens to thousands. Every time I need to show a graph, I know what N is, however, since N / M cannot be whole, there is an accumulated error that I want to somehow compensate for.

I work in simple C and no math libraries can be used.

EDIT 2: Data is relatively uniform with peaks once in a while, and I don't want to miss these peaks during interpolation.

EDIT 3: I am looking for a solution that will work well enough for any N larger than M and smaller than M.

Thank.

+3
source share
1 answer

One good solution is not to sort through your input samples, but over your output positions. That is, you will always draw exactly Mpixels. To calculate the closest sample value for the iith pixel, use the array offset:

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

, ( , N ). , N , M, , , 1 ( , , ). , , , ( - - , ), , , .

+4
source

All Articles