9
$\begingroup$

Note: This questions is quite different from the ones referred to in the comments. Those deal with numerical questions, while this one is algebraic.

I have plots of the following type:

Plot[Cos[50 t] + Cos[51 t], {t, 0, 10}]

enter image description here

I would like to plot a envelope over this plot, i.e. another plot that joins all of maxima and minima of this plot respectively. Here is my attempt, but it's not exactly what I'd like:

Plot[{Cos[50 t] + Cos[51 t], Cos[t] + 1.5, -Cos[t] - 1.5}, {t, 0, 10}]

enter image description here

How can I generate the actual envelope?

$\endgroup$
  • $\begingroup$ I'm guessing that's something like Cos[42x]+Cos[43x]? $\endgroup$ – Mark McClure Jun 3 '14 at 18:29
  • $\begingroup$ this is completely true but this is for the Fig.1, I want to access to sheath while I do not access to any formula for that. $\endgroup$ – Unbelievable Jun 3 '14 at 18:32
  • $\begingroup$ Well, you've got to give us some kind of input to start with. $\endgroup$ – Mark McClure Jun 3 '14 at 18:34
  • $\begingroup$ I plotted these ones with: Plot[{Cos[50 t] + Cos[51 t], Cos[t] + 1.5, -Cos[t] - 1.5}, {t, 0, 10}], I used of simulated functions 'Cos[t] + 1.5' and '-Cos[t] - 1.5' for the sheath. $\endgroup$ – Unbelievable Jun 3 '14 at 18:36
  • 1
    $\begingroup$ Nearly a duplicate of Elegant way of obtaining the envelope of oscillating function, which is a duplicate of Mathematica envelope for the bottom of a plot, a generic function. But this one just straight trigonometry. $\endgroup$ – Michael E2 Aug 28 '14 at 0:20
14
$\begingroup$

Playing with the manipulate below might help. It's based on the the acoustics of beats.

Manipulate[Plot[
  {Cos[a*t] + Cos[b*t], 2*Cos[(b - a) t/2], -2*Cos[(b - a) t/2]}, {t, 0, 10},
  PlotStyle -> {
   Directive[Opacity[0.7]], 
   Directive[Black, Thick], 
   Directive[Black, Thick]}],
 {{a, 20}, 1, 50}, {{b, 21}, 1, 50}]

enter image description here

$\endgroup$
  • $\begingroup$ Thank you so much. this is correct $\endgroup$ – Unbelievable Jun 3 '14 at 18:58
15
$\begingroup$

Don't mind me, I'm just having fun.

Grab the definition of HilbertTransform from this previous post, and then:

f[t_] := Cos[50 t] + Cos[51 t] + Sin[53 t] (* more sinusoids = more fun *)
g[t_] := Evaluate@HilbertTransform[f[τ], τ, t]
h[t_] := Abs[f[t] + I g[t]]
Plot[{f[t], h[t], -h[t]}, {t, 0, 10}, ImageSize -> Large, PlotPoints -> 100,
 PlotStyle -> {Automatic, Black, Black}]

enter image description here

You can see that the envelope has a nice analytical form:

ComplexExpand[h[t]] // FullSimplify

$\sqrt{3 + 2\cos t + 2\sin 2t + 2\sin 3t}$

Further reading: analytic representation.

$\endgroup$
  • $\begingroup$ This way though doesn't seem to catch when the envelope should go to zero. Shouldn't it e.g. vanish near 2? $\endgroup$ – Ruslan Jun 4 '14 at 8:34
  • 1
    $\begingroup$ Should it? It still looks like a sinusoid with small but nonzero amplitude there. (The envelope correctly goes to zero for the original function in the question, if that's what you're concerned about.) $\endgroup$ – Rahul Jun 4 '14 at 8:55
  • $\begingroup$ Ah, OK then, you're right. $\endgroup$ – Ruslan Jun 4 '14 at 9:02
  • $\begingroup$ @Rahul: wow! that's really cool! Would this also work on a set of discrete data? Something like ListLinePlot[Accumulate[RandomReal[{-1, 1}, 1000]]] ... If not, how could one modify it so that it does? I have a bunch of data that makes a seemingly random curve like that and I'd like be able to integrate over some area under the curve like the one that you generated. (Hmmm, i just copy pasted your code, but didn't get the black curve :( what could have gone wrong?) $\endgroup$ – Raksha May 16 '15 at 3:46
  • 1
    $\begingroup$ @Solarmew: You first need to copy the definition of the Hilbert transform from the post I linked to. It also gives a discrete version of the transform, which you could use in much the same way on discrete data. $\endgroup$ – Rahul May 16 '15 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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