# resolving difference between simulink results and manually calculated EXCEL results

#### derick007

Joined Feb 5, 2013
11
Please find my attempt to manually calculate the dft of the Hann function.

I have built a model in MATLAB - Simulink in order to verify the results and included them in the attachment. As you can see the is a substantial difference ?

Apparently the amplitude at zero frequency/dc should be 1v, 1w, 0dB or 30dBw.

I cannot understand this as the HANN function in the time domain, clearly produces a dc amplitude of 0.5v, 0.25w, -6dB or 24dBw.

Any help at all would be very welcome.

I can email the original spreadsheet to help with the calculations.

Regards, Derek

#### Attachments

• 84.4 KB Views: 8
• 19.7 KB Views: 2
• 246.7 KB Views: 6

#### Papabravo

Joined Feb 24, 2006
17,297
If I read your post correctly, you are asking if 1v, 1w, 0 dB, and 30 dBw are all referring to the same thing. In my experience that is just not possible. The situation is similar for 0.5v, 0.25 w, -6 dB, and 24 dBw.

The maximum value of the Hann function occurs at the midpoint of the range [0,...,N-1]. This is well defined if N is odd. If N=3, then the midpoint occurs at n=1, with a value of 1.

$$w(n)= \frac{1}{2}(1-cos(\frac{2 \pi n}{N-1}))$$

The value at points 0 and 2 is appropriately 0

Last edited:

#### sailorjoe

Joined Jun 4, 2013
363
Your Hann function seems wrong. You have the same value for middle four time points. Not clear to me perhaps what those values are, but they match your vertical list of values on the right. Does Matlab give you identical values for the Hann function?

#### Papabravo

Joined Feb 24, 2006
17,297
The expressions for n and N are wrong. It looks like N should be 13 and n should be the sequence {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}. Why do you have two copies of each value of n? That is, why is your sequence {0, 0, 1, 1, 2, 2,....}? That way is the road to numerical perdition.

#### derick007

Joined Feb 5, 2013
11
Apologies, I am by no means an expert in excel and have repeated each n value so that I can easily copy and paste the formulae for the real and imaginary parts of the DFT.

N=8 and n=0-7.

#### Papabravo

Joined Feb 24, 2006
17,297
Apologies, I am by no means an expert in excel and have repeated each n value so that I can easily copy and paste the formulae for the real and imaginary parts of the DFT.

N=8 and n=0-7.
In which case, because N is even, the Hann function does not have a unique maximum, but it is symmetrical around the vertical line n=3.5, and should have identical values for n=3 and n=4.
$$w(3) = \frac {1}{2} (1 - cos(\frac{2 \pi (3)}{N-1})) \approx 0.9504$$
$$w(4) = \frac {1}{2} (1 - cos(\frac{2 \pi (4)}{N-1})) \approx 0.9504$$

$$w(3.5) = \frac {1}{2} (1 - cos(\frac{2 \pi (3.5)}{N-1})) = 1.0000 \; \text exact$$

#### sailorjoe

Joined Jun 4, 2013
363
Yes, precisely. Again, what does Matlab have to say about the Hann window values? Yours should be identical, no?

#### derick007

Joined Feb 5, 2013
11
My calculations, for the HANN function, agree with yours above.

I then multiply ( * ) the Hann function with a constant unit value of 1v x(n), which obviously is just the Hann function :

x(n) * w(n) = w(n) and use this in the DFT summation :

DFT = X(k) = ∑ { x(n) * w(n) } ( cos (2πnk/N) - j sin (2πnk/N) ).

This is were my results do not agree with MATLAB/Simulink, for k=0 I get | X(0) | = 3.5, | X(0)/N ) = 0.4375v, | X(0)^2 | / | N^2 | = 0.191 watts =
22.82 dBm. My Model in Simulink on the spectrum analyser = 30 dBm.

#### Papabravo

Joined Feb 24, 2006
17,297
I can't find any error in the computation, described above, in the spreadsheet. Do you have any evidence that the Simulink model is doing the identical calculation? This would be from the documentation

#### derick007

Joined Feb 5, 2013
11
Unfortunately I have very little knowledge/experience of MATLAB/Simulink and I am classified as a home user which does not entitle me to any technical support.

My simulink model is very basic and consists of a pulse generator, an oscilloscope and spectrum analyser. Like yourself I suspect there is something wrong in the model but I cannot figure out what it is. I have tried posting on the MATLAB/Simulink forums page a few times in the past but never got any replies.

I have setup the pulse generator to output a constant 1v, which I have verified on the oscilloscope and setup the spectrum analyser with HANN weighting.

#### Papabravo

Joined Feb 24, 2006
17,297
How exactly do you setup the Spectrum analyzer with Hann Weighting?
What exactly does the pulse generator do?
I suspect that something you are doing with Matlab/Simulink is causing the discrepancy

#### sailorjoe

Joined Jun 4, 2013
363
If you want Matlab to simulate what you're doing with your spreadsheet, you need a pulse length of infinity, and a Hann window defined as your windowing function in Matlab. Can you see why?

#### derick007

Joined Feb 5, 2013
11
As part of the spectrum analyser set up there is a WINDOW option which contains HANN, HAMMING, RECTANGULAR, CHEBYSHEV etc.
You have to select one of these options and select a simulation time, over which the analyser calculates the DFT. I think you must use the RECTANGULAR option if you do not want any "shaping".

I have programmed the pulse generator to output a constant amplitude of 1v over the simulation time of 0.5ms - I can see this on the oscilloscope.

I think I understand why the pulse has to have an infinite period - to obey the Fourier maths. In reality the spectrum analyser truncates the infinite period which will produce a different spectrum. The case is well demonstrated by using a simple sinewave. Fourier maths gives a single amplitude at a +ve and -ve frequency, whilst the spectrum analyser produces sinc functions at these frquencies.

This could be the source of my problem. I will try and adjust my model to produce a pulse of infinite period with HANN weighting over a time period equal to the simulation time.

#### sailorjoe

Joined Jun 4, 2013
363
I think you're on the right track. In real life you can't escape the window problem. So a variety of adjusted window shapes have been developed to try to minimize the impact of the finite data sample versus the infinite data that Fourier is based on. Also remember that you can exchange the window and the signal and the results are identical.

#### derick007

Joined Feb 5, 2013
11
For information I have sent the following to the Mathworks (MATLAB) forum :

"I have built a simple model consisting of a CONSTANT source, of amplitude 1 and a sample time of 62.5 us and simulation time of 500 us with an oscilloscope and spectrum analyser i.e. N=8. The spectrum analyser has been setup with HANN weighting and 0% overlap and inherited sample rate with a power spectrum (dBm).The scale is linear with 0 Hz offset and a reference load and averages of 1.

I would like to know what settings should a choose in the Main Options for RBW/Window length and subsequent sub settings e.g. NFFT etc."

#### derick007

Joined Feb 5, 2013
11
As usual any replies from mathworks are painfully slow and do not answer the point in question .
In my example of N=8,
X(1) = -1.92 -0.8i
|X(1)| = 2.08
|X(1)|/N = 0.26
POWER (w) = |X(1)|^2/N^2 = 0.0676 w = 30 + (10*log(0.0676)) = 18.3 dBm
My guess is Simulink's spectrum analyser does a similar conversion, but comes up with 23.979 dBm for a double sided spectrum and 26.99 dBm for single sided.

#### derick007

Joined Feb 5, 2013
11
Please find below my latest thoughts on how to resolve the differences I find between my efforts and MATLAB - Simulink on the power spectra for the HANN function :

Assuming a 1 ohm load, f(t) = 0.5 – 0.5 cos2πft

DC Component = 0.5v = 0.25w = -6.02 dB = 23.979 dBm

SINUSOID = -0.5 cos2πft

Using Eulers formula this will give an amplitude spectra of 0.5/2 v @ +/- f.

Therefore amplitude @ +/- f = 0.25v = 0.0625w = -12.04 dB = 17.96 dBm.

Amplitude @ +f only = 0.5v = 0.125w = -9.03 dB = 20.969 dBm.

Note for A sin2πft, amplitude = A/2 @ +/- f and A @ +f only. Power = A^2/4 @ +/- f and A^2/2 @ +f only.

NORMALISATION :

DC = 23.979 dBm + 6.021 dm = 30 dBm – AGREES WITH SIMULINK

SINUSOID @ +/- f = 17.958 dBm + 6.021 dBm = 23.979 dBm – AGREES WITH SIMULINK

SINUSOID @ + f = 20.969 dBm + 6.021 dBm = 26.99 dBm – AGREES WITH SIMULINK

However, there are still a few questions :

1. The above really depends on Simulink “normalising” the results.

2. The Simulink results were obtained from a sampled analogue signal with N=8. I will try increasing the number of samples to 16, 32 etc. and see what effect it has. I was under the illusion that increasing the number of samples would ensure the Simulink results change and get closer to the “analogue” results.

3. I noticed the spectrum analyser plot in Simulink is a continuous line ? I would have thought the should be a discrete set of samples ?

#### Papabravo

Joined Feb 24, 2006
17,297
I think you are onto something. Keep up the sleuthing.

#### derick007

Joined Feb 5, 2013
11
I can see the advantage in normalising data when a comparison between e.g. HANN and RECTANGULAR windowing is required.

However in my case I was only interested in the effect of HANN windowing and therefore on absolute values.

I can only assume normalising data in such cases is "industry" practise and that MATLAB/Simulink only generate normalised results.

I have tried contacting MATLAB on several ocassions but I have found their technical support to be slow and confusing.

#### Papabravo

Joined Feb 24, 2006
17,297
Yeah. If you're not a big institutional customer, you might as well be chopped liver. At least Scilab is still free.