![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2004
Posts: 18
Rep Power: 0
![]() |
Suppose I have an array X of short ints with around a several thousand values.
What I want to do is generate another array Y of exactly 1000 elements whose content is representative of X. Y should also be an array of short ints. But because X represents amplitudes of sound, I don't want to do uniform sampling because I may miss certain important peaks. I also don't want to use the naive approach of reading in a chunk of X, finding the average or max of that chunk and using that result as an element of Y, and repeating the process until I've got 1000 values. I call it naive because I recently bumped into an interesting concept called Root Mean Square, which, as google informs me, is perfect for waves (including soundwaves. So! What I want to do is a read in a chunk of X, get the RMS square of that chunk and use that. Somehow what I have got so far ends up with unsatisfying result of getting back an array with all elements with the value 0. Here's my code: short int* calculatePoints(short int speakerData[], long arraySize)
{
//how many values to plot along x-axis
int horizontalResolution = 1024;
//declare return value array
short int* GraphPoints;
GraphPoints = new short int[horizontalResolution];
/* 0 this array out first to make sure we don't get any old data
re-appearing again */
for (int G=0; G<horizontalResolution; G++)
GraphPoints[G]=0;
//calculate how many samples each pixel is representative of
int bufferSize = arraySize / horizontalResolution;
if ( bufferSize % 2 ) bufferSize++;
long i = 0; //index to current value in old array
int x = 0; //index to pixel for the current iteration
double RMS = 0; //variable for ROOT MEANS SQUARE calculation
while ( x < 1024 )
{
//read in a chunk, summing the squares of each value
for (int j=i; j<bufferSize; j++)
RMS = RMS + (PCM_Data[i+j]*PCM_Data[i+j]);
RMS = RMS / bufferSize; //get the mean
RMS = sqrt(RMS); //get the square root
GraphPoints[x]=(short int) RMS; //store the value
i=i+bufferSize; //skip a few values
x++; //increment the loop counter
}
return GraphPoints;
}Now, I'm guessing that the cast from double back to short int maybe causing a problem, but I'm not sure -- any ideas? |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
Try adding tests to make sure the values are getting to the two functions...
printf("Pre-sqrt: %f\n", RMS);
RMS = sqrt(RMS); //get the square root
printf("Pre-cast: %f\n\n", RMS);
GraphPoints[x]=(short int) RMS; //store the value
__________________
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Nov 2004
Posts: 18
Rep Power: 0
![]() |
thanks it works now. stupid fence post errors
![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|