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

Submission of ratio table for windowing function #40

Open
MarScaper opened this issue Jan 18, 2020 · 4 comments
Open

Submission of ratio table for windowing function #40

MarScaper opened this issue Jan 18, 2020 · 4 comments

Comments

@MarScaper
Copy link
Contributor

MarScaper commented Jan 18, 2020

Thanks for your work. I see that ratio table is in your todo list.

I calculated the table for my own need. Here It is...

float WindowCompensationFactor[10] =
{
    2,            /* rectangle (Box car) */
    1.8549343278*2, /* hamming */
    1.8554726898*2, /* hann */
    2.0039186079*2, /* triangle (Bartlett) */
    2.8163172034*2, /* nuttall */
    2.367347436*2,  /* blackman */
    2.7557840395*2, /* blackman nuttall */
    2.7929062517*2, /* blackman harris*/
    3.5659039231*2, /* flat top */
    1.5029392863*2  /* welch */
};

Compensation seems to be fine in my project... :)
Simulation screenshot

Hope It helps,
Sebastien.

@kosme
Copy link
Owner

kosme commented Jan 22, 2020 via email

@MarScaper
Copy link
Contributor Author

In my opinion, the best approach would be to allow compensation within the Windowing() method with a conditional parameter for backward compatibility.

Header could be changed like this...

void Windowing(uint8_t windowType, uint8_t dir, bool compensation=false);

And method implementation like this...

void arduinoFFT::Windowing(uint8_t windowType, uint8_t dir)
{// Weighing factors are computed once before multiple use of FFT
// The weighing function is symetric; half the weighs are recorded
	double samplesMinusOne = (double(this->_samples) - 1.0);
	for (uint16_t i = 0; i < (this->_samples >> 1); i++) {
		double indexMinusOne = double(i);
		double ratio = (indexMinusOne / samplesMinusOne);
		double weighingFactor = 1.0;
		// Compute and record weighting factor
		switch (windowType) {
		case FFT_WIN_TYP_RECTANGLE: // rectangle (box car)
			weighingFactor = 1.0;
			break;
		case FFT_WIN_TYP_HAMMING: // hamming
			weighingFactor = 0.54 - (0.46 * cos(twoPi * ratio));
			break;
		case FFT_WIN_TYP_HANN: // hann
			weighingFactor = 0.54 * (1.0 - cos(twoPi * ratio));
			break;
		case FFT_WIN_TYP_TRIANGLE: // triangle (Bartlett)
			weighingFactor = 1.0 - ((2.0 * abs(indexMinusOne - (samplesMinusOne / 2.0))) / samplesMinusOne);
			break;
		case FFT_WIN_TYP_NUTTALL: // nuttall
			weighingFactor = 0.355768 - (0.487396 * (cos(twoPi * ratio))) + (0.144232 * (cos(fourPi * ratio))) - (0.012604 * (cos(sixPi * ratio)));
			break;
		case FFT_WIN_TYP_BLACKMAN: // blackman
			weighingFactor = 0.42323 - (0.49755 * (cos(twoPi * ratio))) + (0.07922 * (cos(fourPi * ratio)));
			break;
		case FFT_WIN_TYP_BLACKMAN_NUTTALL: // blackman nuttall
			weighingFactor = 0.3635819 - (0.4891775 * (cos(twoPi * ratio))) + (0.1365995 * (cos(fourPi * ratio))) - (0.0106411 * (cos(sixPi * ratio)));
			break;
		case FFT_WIN_TYP_BLACKMAN_HARRIS: // blackman harris
			weighingFactor = 0.35875 - (0.48829 * (cos(twoPi * ratio))) + (0.14128 * (cos(fourPi * ratio))) - (0.01168 * (cos(sixPi * ratio)));
			break;
		case FFT_WIN_TYP_FLT_TOP: // flat top
			weighingFactor = 0.2810639 - (0.5208972 * cos(twoPi * ratio)) + (0.1980399 * cos(fourPi * ratio));
			break;
		case FFT_WIN_TYP_WELCH: // welch
			weighingFactor = 1.0 - sq((indexMinusOne - samplesMinusOne / 2.0) / (samplesMinusOne / 2.0));
			break;
		}

		if( compensation ){
			weighingFactor *= WindowCompensationFactor[windowType];
		}

		if (dir == FFT_FORWARD) {
			this->_vReal[i] *= weighingFactor;
			this->_vReal[this->_samples - (i + 1)] *= weighingFactor;
		}
		else {
			this->_vReal[i] /= weighingFactor;
			this->_vReal[this->_samples - (i + 1)] /= weighingFactor;
		}
	}
}

Easy to set and no impact on existing code for users.

@HorstBaerbel
Copy link

See the develop branch and / or pull request #42 where this is implemented.

@kosme
Copy link
Owner

kosme commented Dec 28, 2023

@MarScaper do you agree with the statement on issue #61 ??

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

3 participants