# how can I analyze a basic sine wave for its frequency phase and amplitude at a particular point in time in matlab

How can I analyze a basic sine wave for its frequency phase and amplitude at a particular point in time in MATLAB? Are there any tools to do that? I am trying to do additive synthesis of inharmonic sounds in MATLAB, but having a great difficulty to extract the needed phase amplitude and frequency data of the partials I am trying to generate. Can someone shed some light regarding signal analysis in MATLAB?

If your the unknown signal $x(n)$ is modeled as: $$x(n)=A \sin(2 \pi f n+ \phi)+w(n)$$ and you want to estimate $A$,$f$,and $\phi$ accurately, you can use least square estimation. Unfortunately the cost function is nonlinear. You can use nonlinear least square in MATLAB to find the parameters as follows:

Make a cost function:

f=@(A,f,phi) x[n]-A sin(2*pi*  f *n+ phi)


and use

p0=[A0,f0,phi0];

p = lsqnonlin(f,p0);


to find the unknown parameters. Note that the optimizer will have a hard time finding $f$ as the problem is not convex. So it is best if you can give an initial estimate of frequency by using a method like fft.

If the frequency is known then the problem can be converted to a linear estimation as: $$x(n)=A \sin(2 \pi f n+ \phi)+w(n)$$ $$=A \sin(2 \pi f n) \cos (\phi) +A \cos(2 \pi f n) \sin (\phi)+w(n)$$ $$=p_1 S[n] +p_2 C[n]+w(n),$$ where $p_1=A \cos(\phi)$ and $p_2=A \sin(\phi)$ are unknown parameters, and $S[n]$ and $C[n]$ are known.

Here's some Matlab code:

% Assume 'x' is the input time-domain sequence and 'y' is it's Hilbert transform.

Analytic_Signal = hilbert(x);
y = imag(Analytic_Signal);

Instantaneous_Magnitude = abs(Analytic_Signal);

Instantaneous_Phase = unwrap(atan2(y, x));

Instantaneous_Freq = diff(Instantaneous_Phase);


if the sinusoid, $x(t)$, is uncontaminated, i s'pose you can measure all three by first computing the Hilbert transform of it, $\hat{x}(t)$ and, from the two, construct the analytic signal,

$$x_a(t) \ \triangleq \ x(t) \ + \ j\hat{x}(t)$$

the amplitude will be $|x_a(t)|$, the instantaneous phase will be $\phi(t) = \arg\{x_a(t)\}$ and, if the phase is unwrapped the derivative of the phase w.r.t. time is the instantaneous (angular) frequency is this derivative.

$$\omega(t) \ = \ \frac{d}{dt}\phi(t) \ = \ \frac{d}{dt}\arg\{x(t) \ + \ j\hat{x}(t)\}$$

• Would performing the Hilbert transform not introduce an implicit windowing? I'm curious how this might affect the concept of a measured `instantaneous' frequency. – Speedy Jul 3 '14 at 9:43
• @Speedy. I believe robert bristow-johnson is correct. What do you mean by "implicit windowing"? – Richard Lyons Jul 29 '15 at 8:51
• @RichardLyons, I was wondering whether instantaneous frequency is really instantaneous in this case. Since the computation of a Hilbert transform requires a time-base or "implicit window" - (perhaps a poor choice of words), is the computed value not meaningful only for a region of time, rather than at an instant? – Speedy Jul 29 '15 at 14:14
• In fact I find it difficult to understand how a value of "instantaneous" frequency can be meaningful at all. – Speedy Jul 29 '15 at 14:15
• @Speedy. Your implied question, "Does instantaneous frequency have any physical meaning?" is actually a good question. You should have a look at 'frequency modulation (FM) signals' and 'FM demodulation' tutorial material to answer that question. (If instantaneous frequency had no physical meaning we'd have no FM radio. As far as I can tell Robert B-J's scheme is a high-performance way to perform FM demodulation.) Recall, frequency is defined as 'a phase angle change divided by a fixed interval of time'. That is, delta phase over delta time, cycles/second, or radians/second. – Richard Lyons Jul 30 '15 at 12:57