I'm working on a project to record sound from a STM32. I have ADC values raning from 0 to 4095. I can convert it to an audio file and hear my voice. This is my algorithm for getting sample ranges in MATLAB: (ADC Value / 2047.5) - 1
It works great but the problem is that is not standard
Maybe there is a filter that removes the DC component.
It is possible to make that in the Arduino (or STM32).
When you have a average value, created by a low pass filter in software, then you can use that as a offset for every sample.
I make such a low pass filter with 'float' variables, but it is faster with integers. I forgot how to do it with integers.
float filtered;
void setup()
{
filtered = analogRead( ... // start with begin value, to get started at the begin.
...
}
void loop()
{
int adcValue = analogRead( ...
filtered = (0.99 * filtered) + (0.01 * float( adcValue)); // 1 percent low pass filter
int sample = adcValue - int( filtered); // use the filtered value as offset
// The 'sample' will now be a value around zero without DC offset.
...
}
I think that the 1% filter is too much, probably a lower value is better.
Normally you apply a DC offset to the incoming audio so it sits half way through the ADC range. This will give you the biggest range of negative-going and positive-going sound values.
If you subtract 2047 from each value you'll get positive and negative values if that's what you want.
When speaking into a microphone, then the signal is a normal signal with positive and negative peaks "together".
When using a electret microphone connected to a analog input, then it is no longer a good audio signal.
If you want that, then you should make a small circuit with a transistor as pre-amplifier.
Why is that important ? What you want is trivial and might not be needed. You have one signal zoomed in, and the other signal zoomed out. Therefor it seems as if they are different, but they are similar.
Audacity has a filter "Normalize" that has an option to remove the DC offset. Does that look better if you apply that ?
According to Audacity: "Technically, it does this by finding the average of all the sample values in the selection, then subtracting that average value from all the samples". That about the same what I described before. https://manual.audacityteam.org/man/normalize.html
Substract the average from every sample.
The average of all samples is found by adding all sample values and dividing that by the number of samples.
Suppose the samples are -10, 1, 5, -20.
The total of all sample values = -24
The average is -24 / 4 = -6
To normalize the samples, add 6 to all the samples.
The normalized samples are -4, 7, 11, -14.
Check: -4 + 7 + 11 -14 = 0. That is correct, the samples are now around the zero line.