Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 7th, 2004, 7:17 AM   #1
BruceLeroy
Newbie
 
Join Date: Nov 2004
Posts: 18
Rep Power: 0 BruceLeroy is on a distinguished road
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?
BruceLeroy is offline   Reply With Quote
Old Nov 7th, 2004, 11:14 PM   #2
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 6 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
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
__________________

tempest is offline   Reply With Quote
Old Nov 8th, 2004, 1:57 PM   #3
BruceLeroy
Newbie
 
Join Date: Nov 2004
Posts: 18
Rep Power: 0 BruceLeroy is on a distinguished road
thanks it works now. stupid fence post errors
BruceLeroy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:36 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC