What I am after is the center line of this wave, which is 0. How can I calculate that value though?

For example, run the code below for a simple example in Matlab. This will plot a simple sine wave with a couple of periods.

x = linspace(0,7.5*pi,100); y = sin(x); plot(x,y); mean(y)

enter image description here

The result (and the problem) is that mean(y) is 0.0368, and not 0. But I am plotting a sine wave oscillating around 0 of course.

How can I calculate these kind of averages correctly (without me having to plot and actually examine each signal manually to get the periods right)?

  • $\begingroup$ This question has nothing to do with engineering. $\endgroup$
    – Wasabi
    Commented May 11, 2017 at 13:31
  • $\begingroup$ The signals are CFD solver outputs, so it's from an applied engineering background $\endgroup$ Commented May 11, 2017 at 13:59
  • 1
    $\begingroup$ You haven't plotted the wave for "a couple of periods", but for 3.75 periods. If you want the mean to be "what you expect", you need to plot it for number of complete periods, without an extra bit stuck on the end. If you don't know the frequency, then make an FFT of the data in the time domain. $\endgroup$
    – alephzero
    Commented May 11, 2017 at 16:09
  • $\begingroup$ It would be much better to show an actual signal than a basic sine wave. It's not clear from your example A) why you expect an offset from zero or B) why you aren't simply averaging the max and min y-values. $\endgroup$
    – Air
    Commented May 11, 2017 at 18:45

2 Answers 2


The problem that you describe comes from the fact that you calculating the average of the vector with numerical values. In order to calculate the exact mean of $\sin(x)$ or other functions, you should use computer algebra systems like (MATLAB symolic toolbox which is not always exact, Mupad in MATLAB, Maple, Mathematica, sympy package for Python).

Another problem is that you want to calculate the mean of $\sin(x)$ on the interval from $0$ to $7.5 \pi$. This should acutally not give you a value of $0$.

The mean $<f(x)>$ of a function $f(x)$ on the interval $a$ to $b$ is given as: $$<f(x)>_{[a,b]} = \frac{1}{b-a}\int_{a}^{b}f(x)dx.$$

For your problem this leads to:

$$<\sin(x)>_{[0,7.5\pi]}=\frac{1}{7.5\pi-0}\int_{0}^{7.5\pi}\sin(x)dx=\frac{1}{7.5\pi}\left[\cos(7.5\pi)-\cos(0)\right]\approx \frac{12}{90\pi}\approx 0.04244...$$

Here is the MATLAB code:

syms x;
1/(7.5*pi-0)*int(sin(x), x, 0, 7.5*pi)

Note that the mean is not exactly correct because MATLAB is using some weird fraction.

If you do not care about 100% precision, then you could also use more points:

  • $\begingroup$ Thanks for your answer, but the problem is that I don't know exactly the range of the interval! 0 to 7.5*pi was just an example, but it could have been 1.5 to 11.85*pi. Yes, the calculated mean is right, but we (as humans) can see that the signal oscillates around 0! $\endgroup$ Commented May 11, 2017 at 15:42
  • 1
    $\begingroup$ No, we as humans don't see that the signal is oscillating around 0, you are guessing that the mean has some value. Actually, you can not deduce the mean from a simple plot, the exact mean could be anything. And the mean value of your function is not $0$. The mean value is only $0$ if you are averaging over a period of the function you want to calculate the mean. $\endgroup$
    – MrYouMath
    Commented May 11, 2017 at 15:53
  • $\begingroup$ @fragmachine The value you're talking about finding by inspection is not the mean of the plot. It may be the limit of the mean of the plot as the bounds approach infinity, but that's something else entirely. What you're looking for might be better described as the offset of the wave, the center line of the wave, the neutral line of the wave. The terminology is inexact; "offset" in particular is often used to mean "phase shift" so you might want to be more explicit, e.g. say "vertical offset"... As to whether you actually need the mean or this other value, maybe you can clarify? $\endgroup$
    – Air
    Commented May 11, 2017 at 16:48
  • 1
    $\begingroup$ @Air Yes, the wave's center line is exactly what I mean and what I need! The "mean" in this case is just an approximation for this center line of the wave. I was also thinking of using FFT to determine the center line somehow, but I am not sure how this works yet. $\endgroup$ Commented May 11, 2017 at 17:59
  • $\begingroup$ @fragmachine: This is a different question. You should post this as a new question. $\endgroup$
    – MrYouMath
    Commented May 11, 2017 at 18:10

You could keep the entire computation symbolic and defer computing for specific ranges.



From $0$ to $7.5 \pi$:

mean/.{xmin->0,xmax->7.5 \[Pi]}


enter image description here

From $1.5 \pi$ to $ 11.85 \pi$:

mean /. {xmin -> 1.5 \[Pi], xmax -> 11.85 \[Pi]}


enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.