# is normalizing FFT magn spectrum different for Nyquist-frequency's bin?

Started by November 3, 2010
```Hi Experts,

Let's say I compute the FFT magnitude spectrum of a variable X(n) in
Matlab, where X(n) has indices n running from 1 to N.

The magnitude spectrum is plotted for indices n = 2 (ignoring DC index n=1
for now) to n = N/2 + 1 (e.g. the Nyquist frequency bin).

To get the normalization correct, should the Nyquist-frequency bin's
magnitude be cut in half, because the Nyquist bin is not mirrored/reflected
like the other bins (except DC bin)?

That is, is the following normalization correct for Nyquist-bin?

fft_X = fft(X, N);
fftmag_X(2:N/2) = (2/N) * abs(fft_X(2:N/2));
fftmag_X(N/2+1) = (1/N) * abs(fft_X(N/2+1));
plot(fftmag_X(2:N/2+1));

If so, I suppose the following normalization is also valid for plotting the
magnitude spectrum, for viewing the relative magnitudes between all of the
bins (i.e. don't need to divide by N as part of normalization):

fft_X = fft(X, N);
fftmag_X(2:N/2) = 2 * abs(fft_X(2:N/2));
fftmag_X(N/2+1) = 1 * abs(fft_X(N/2+1));
plot(fftmag_X(2:N/2+1));

Does that sound right? Thanks in advance.
```
```all4dsp <all4dsp@n_o_s_p_a_m.comcast.net> wrote:

> Let's say I compute the FFT magnitude spectrum of a variable X(n) in
> Matlab, where X(n) has indices n running from 1 to N.

Assuming Matlab works the usual way...

> The magnitude spectrum is plotted for indices n = 2 (ignoring DC index n=1
> for now) to n = N/2 + 1 (e.g. the Nyquist frequency bin).

> To get the normalization correct, should the Nyquist-frequency bin's
> magnitude be cut in half, because the Nyquist bin is not mirrored/reflected
> like the other bins (except DC bin)?

I don't believe so.

Consider the IFFT with as input all zeros except for the N/2+1 bin.
Then consider all zeros except for one other bin, other than n=1.
Compare the results, such as computing the RMS value.

There is some asymmetry in the FFT/IFFT regarding n=1 and n=N/2+1.

-- glen
```
```>all4dsp <all4dsp@n_o_s_p_a_m.comcast.net> wrote:
>
>> Let's say I compute the FFT magnitude spectrum of a variable X(n) in
>> Matlab, where X(n) has indices n running from 1 to N.
>
>Assuming Matlab works the usual way...
>
>> The magnitude spectrum is plotted for indices n = 2 (ignoring DC index
n=1
>> for now) to n = N/2 + 1 (e.g. the Nyquist frequency bin).
>
>> To get the normalization correct, should the Nyquist-frequency bin's
>> magnitude be cut in half, because the Nyquist bin is not
mirrored/reflected
>> like the other bins (except DC bin)?
>
>I don't believe so.
>
>Consider the IFFT with as input all zeros except for the N/2+1 bin.
>Then consider all zeros except for one other bin, other than n=1.
>Compare the results, such as computing the RMS value.
>
>There is some asymmetry in the FFT/IFFT regarding n=1 and n=N/2+1.
>
>-- glen

Thanks Glen,

So then, both

fftmag_X(2:N/2+1) = (2/N) * abs(fft_X(2:N/2+1));

and,

fftmag_X(2:N/2+1) = abs(fft_X(2:N/2+1));

would give correct spectrums, where the Nyquist bin (n=N/2+1) is normalized
the same as all the other bins (except DC, n=1, bin)?
```
```On Nov 3, 1:55&#2013266080;pm, glen herrmannsfeldt <g...@ugcs.caltech.edu> wrote:
> all4dsp <all4dsp@n_o_s_p_a_m.comcast.net> wrote:
> > Let's say I compute the FFT magnitude spectrum of a variable X(n) in
> > Matlab, where X(n) has indices n running from 1 to N.
>
> Assuming Matlab works the usual way...
>
> > The magnitude spectrum is plotted for indices n = 2 (ignoring DC index n=1
> > for now) to n = N/2 + 1 (e.g. the Nyquist frequency bin).
> > To get the normalization correct, should the Nyquist-frequency bin's
> > magnitude be cut in half, because the Nyquist bin is not mirrored/reflected
> > like the other bins (except DC bin)?
>
> I don't believe so.
>
> Consider the IFFT with as input all zeros except for the N/2+1 bin.
> Then consider all zeros except for one other bin, other than n=1.
> Compare the results, such as computing the RMS value.
>
> There is some asymmetry in the FFT/IFFT regarding n=1 and n=N/2+1.

boy, isn't it just obvious that MATLAB screwed up the indexing
convention on this?  now, in order to speak "Matlab", Glen is
accommodating their dumb and hard-wired indexing origin by adding 1 to
everything.  it's bad enough we have to do that to communicate to
MATLAB, it's just ridiculous that, for the sake of context, we end up
doing so to communicate to each other.

just a shame.

r b-j
```
```In comp.dsp robert bristow-johnson <rbj@audioimagination.com> wrote:
(snip, then I wrote)

>> There is some asymmetry in the FFT/IFFT regarding n=1 and n=N/2+1.

> boy, isn't it just obvious that MATLAB screwed up the indexing
> convention on this?  now, in order to speak "Matlab", Glen is
> accommodating their dumb and hard-wired indexing origin by adding 1 to
> everything.  it's bad enough we have to do that to communicate to
> MATLAB, it's just ridiculous that, for the sake of context, we end up
> doing so to communicate to each other.

Well, I started Fortran programming before 1977, when array
started at 1.  I did find C array indexing starting at 0 to be
much more natural much of the time, though.

Still, it isn't so obvious by now that Matlab shouldn't have an
option for array origin.

-- glen
```
```>all4dsp <all4dsp@n_o_s_p_a_m.comcast.net> wrote:
>
>> Let's say I compute the FFT magnitude spectrum of a variable X(n) in
>> Matlab, where X(n) has indices n running from 1 to N.
>
>Assuming Matlab works the usual way...
>
>> The magnitude spectrum is plotted for indices n = 2 (ignoring DC index
n=1
>> for now) to n = N/2 + 1 (e.g. the Nyquist frequency bin).
>
>> To get the normalization correct, should the Nyquist-frequency bin's
>> magnitude be cut in half, because the Nyquist bin is not
mirrored/reflected
>> like the other bins (except DC bin)?
>
>I don't believe so.
>
>Consider the IFFT with as input all zeros except for the N/2+1 bin.
>Then consider all zeros except for one other bin, other than n=1.
>Compare the results, such as computing the RMS value.
>
>There is some asymmetry in the FFT/IFFT regarding n=1 and n=N/2+1.
>
>-- glen
>

I wrote a short program in Matlab just now to try and answer this, and it
appears the Nyquist-frequency bin does need to be normalized by multiplying
by 0.5 compared to other non-DC frequency bins, in order to compare
relative magnitudes in an FFT magnitude plot. I suppose most people don't
worry about such details as the anti-alias filter removes energy at
Nyquist.
```
```On Nov 3, 3:34&#2013266080;pm, "all4dsp" <all4dsp@n_o_s_p_a_m.comcast.net> wrote:
> Hi Experts,
>
> Let's say I compute the FFT magnitude spectrum of a variable X(n) in
> Matlab, where X(n) has indices n running from 1 to N.
>
> The magnitude spectrum is plotted for indices n = 2 (ignoring DC index n=1
> for now) to n = N/2 + 1 (e.g. the Nyquist frequency bin).
>
> To get the normalization correct, should the Nyquist-frequency bin's
> magnitude be cut in half, because the Nyquist bin is not mirrored/reflected
> like the other bins (except DC bin)?
>
> That is, is the following normalization correct for Nyquist-bin?
>
> fft_X = fft(X, N);
> fftmag_X(2:N/2) = (2/N) * abs(fft_X(2:N/2));
> fftmag_X(N/2+1) = (1/N) * abs(fft_X(N/2+1));
> plot(fftmag_X(2:N/2+1));
>
> If so, I suppose the following normalization is also valid for plotting the
> magnitude spectrum, for viewing the relative magnitudes between all of the
> bins (i.e. don't need to divide by N as part of normalization):
>
> fft_X = fft(X, N);
> fftmag_X(2:N/2) = 2 * abs(fft_X(2:N/2));
> fftmag_X(N/2+1) = 1 * abs(fft_X(N/2+1));
> plot(fftmag_X(2:N/2+1));
>
> Does that sound right? Thanks in advance.

Yes, except when N is odd.

Hope this helps.

Greg
```
```On Nov 7, 1:08&#2013266080;pm, Greg Heath <he...@alumni.brown.edu> wrote:
> On Nov 3, 3:34&#2013266080;pm, "all4dsp" <all4dsp@n_o_s_p_a_m.comcast.net> wrote:
>
>
>
>
>
> > Hi Experts,
>
> > Let's say I compute the FFT magnitude spectrum of a variable X(n) in
> > Matlab, where X(n) has indices n running from 1 to N.
>
> > The magnitude spectrum is plotted for indices n = 2 (ignoring DC index n=1
> > for now) to n = N/2 + 1 (e.g. the Nyquist frequency bin).
>
> > To get the normalization correct, should the Nyquist-frequency bin's
> > magnitude be cut in half, because the Nyquist bin is not mirrored/reflected
> > like the other bins (except DC bin)?
>
> > That is, is the following normalization correct for Nyquist-bin?
>
> > fft_X = fft(X, N);
> > fftmag_X(2:N/2) = (2/N) * abs(fft_X(2:N/2));
> > fftmag_X(N/2+1) = (1/N) * abs(fft_X(N/2+1));
> > plot(fftmag_X(2:N/2+1));
>
> > If so, I suppose the following normalization is also valid for plotting the
> > magnitude spectrum, for viewing the relative magnitudes between all of the
> > bins (i.e. don't need to divide by N as part of normalization):
>
> > fft_X = fft(X, N);
> > fftmag_X(2:N/2) = 2 * abs(fft_X(2:N/2));
> > fftmag_X(N/2+1) = 1 * abs(fft_X(N/2+1));
> > plot(fftmag_X(2:N/2+1));
>
> > Does that sound right? Thanks in advance.
>
> Yes, except when N is odd.
>
> Hope this helps.
>
> Greg- Hide quoted text -
>
> - Show quoted text -

Greg,

The real problem here is that if there is a bin at exactly Nyquist
(i.e. FFT size is even) the bin magnitude does not determine the
magnitude of the signal component at exactly Nyquist. Ex: If I sample
a real sinusoid at Nyquist I can get all zeros, alternating max/min
values, or anything in between.

What can you say from the result? You can figure out a lower bound on
the magnitude.  Not real useful for spectral estimation no matter how
you normalize it.

Dirk
```
```On Sun, 7 Nov 2010 12:18:24 -0800 (PST), Dirk Bell
<bellda2005@cox.net> wrote:

[Snipped by Lyons]
>
>The real problem here is that if there is a bin at exactly Nyquist
>(i.e. FFT size is even) the bin magnitude does not determine the
>magnitude of the signal component at exactly Nyquist. Ex: If I sample
>a real sinusoid at Nyquist I can get all zeros, alternating max/min
>values, or anything in between.
>
>What can you say from the result? You can figure out a lower bound on
>the magnitude.  Not real useful for spectral estimation no matter how
>you normalize it.
>
>Dirk

Hi Dirk,
Good post!

Yes, the spectral magnitude of a sinusoid whose
freq is Fs/2 depends on the initial time-domain
phase of the sinusoid.

See Ya',
[-Rick-]

```
```>On Sun, 7 Nov 2010 12:18:24 -0800 (PST), Dirk Bell
><bellda2005@cox.net> wrote:
>
>              [Snipped by Lyons]
>>
>>The real problem here is that if there is a bin at exactly Nyquist
>>(i.e. FFT size is even) the bin magnitude does not determine the
>>magnitude of the signal component at exactly Nyquist. Ex: If I sample
>>a real sinusoid at Nyquist I can get all zeros, alternating max/min
>>values, or anything in between.
>>
>>What can you say from the result? You can figure out a lower bound on
>>the magnitude.  Not real useful for spectral estimation no matter how
>>you normalize it.
>>
>>Dirk
>
>Hi Dirk,
>  Good post!
>
>Yes, the spectral magnitude of a sinusoid whose
>freq is Fs/2 depends on the initial time-domain
>phase of the sinusoid.
>
>See Ya',
>[-Rick-]
>

Thanks everyone for clarifying what's going on at the Nyquist-frequency
bin. I have a much better understanding now.
```