Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should removing DC component (substracting average) be after applying window? #20

Open
zephray opened this issue Mar 18, 2022 · 3 comments

Comments

@zephray
Copy link

zephray commented Mar 18, 2022

I noticed that in the current code (https://github.com/endolith/waveform_analysis/blob/master/waveform_analysis/thd.py#L61) the DC component is removed before applying window. The calculated DC offset would be different after the window has been applied, causing the resulting FFT to have a rather large DC component in some cases.

For example, I have a 1kHz -3dBFS sine tone 32-bit wave file at 48kHz sample rate. I feed only the first 16K samples to the code (obviously the last sine cycle won't be complete, and that's why window functions are used). The result I got is a THD+N of -77.8dB while I expect it to be much better. If I increase the FFT points to 1M point, the THD+N gets improved to <-100dB. By inspecting the noise spectrum in the 16K point case, I noticed that the DC bin has a -77.8dB magnitude, explaining the rather poor THD+N. If I move the subtraction to after the windowing (windowed -= mean(windowed)), the THD+N immediately gets improved to -189.7dB, and the performance is consistent across different FFT sizes between 16K and 2M. (while in the previous cases, performances improve with larger FFT size).

These make sense to me however I am not sure if my modifications are correct or not. Do you have any idea about this?

@endolith
Copy link
Owner

@zephray Sorry for the delay.

Well, it needs to be removed before windowing or else the DC gets through the window:

Figure_1

Should it also be removed after windowing? Yeah, I think that's fine. DC shouldn't be part of the THD measurement. Analog THD+N meters have HPF at some point to remove it.

@endolith
Copy link
Owner

Probably the right way to do it is to emulate an analog analyzer and apply a bandpass filter with customizable cutoff frequencies

@zephray
Copy link
Author

zephray commented Apr 12, 2022

Thanks for the reply. Yes I later noticed this is also not correct, and ended up implementing an IIR HPF with 20Hz cutoff before sending the signal to windowing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants