Draw waveform - convert to DB squash it

I have a wave file, I have a function that extracts 2 samples per pixel, and then draw lines with them. quickly and painlessly before I get down to scaling. I can display amplitude values ​​without problems

enter image description here

which is an accurate representation of the waveform. for 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 this is great, but I need to display in db, so the quieter wave images arent ridiculously small, but when I do the following change to the above code

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

The wave image is as follows.

enter image description here

which is not accurate, it looks like it is crushed. Is there something wrong with what I'm doing? I need to find a way to convert from amplitude to decibels while maintaining momentum. im thinking that I should not take the average value when converting to DB.

+1
source share
2 answers

Do not convert to dB for reviews. No one does it.

Instead of finding the average value over the block, you should find the maximum absolute value. By averaging, you will lose large amplitude at high-frequency peaks.

+2
source

How can I print a sound in Java Swing?

0
source

Source: https://habr.com/ru/post/885856/


All Articles