drawing waveform - converting to DB squashes it

2020-02-13 06:00发布

问题:

I have a wave file, i have a function that retrieves 2 samples per pixel then i draw lines with them. quick and painless before i deal with zooming. i can display the amplitude values no problem

that is an accurate image of the waveform. to do this i used the following code

//tempAllChannels[numOfSamples] holds amplitude data for the entire wav
//oneChannel[numOfPixels*2] will hold 2 values per pixel in display area, an average of min amp, and average of max
for(int i = 0; i < numOfSamples; i++)//loop through all samples in wave file
{
    if (tempAllChannels[i] < 0) min += tempAllChannels[i];//if neg amp value, add amp value to min
    if (tempAllChannels[i] >= 0) max += tempAllChannels[i]; 

    if(i%factor==0 && i!=0)     //factor is (numofsamples in wav)/(numofpixels) in display area
    {
        min = min/factor;       //get average amp value
        max = max/factor;
        oneChannel[j]=max;
        oneChannel[j+1]=min;
        j+=2;                   //iterate for next time
        min = 0;                //reset for next time
        max = 0;
    }   
}

and that's great but I need to display in db so quieter wave images arent ridiculously small, but when i make the following change to the above code

oneChannel[j]=10*log10(max);
oneChannel[j+1]=-10*log10(-min);

the wave image looks like this.

which isnt accurate, it looks like its being squashed. Is there something wrong with what I'm doing? I need to find a way to convert from amplitude to decibels whilst maintaining dynamics. im thinking i shouldnt be taking an average when converted to DB.

回答1:

Don't convert to dB for overviews. No one does that.

Instead of finding the average over a block, you should find the max of the absolute value. By averaging, you will loose a lot of amplitude in your high frequency peaks.