-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_LSSA
40 lines (35 loc) · 1.72 KB
/
test_LSSA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
'''
Author: Sonal Ranjit
This script transforms a give time series to the frequency domain using the method of
Least Squares. The main concept here is to determine the coefficients of a sum of
sine and cosines functions that best fit the given time series.
The function to fit in the problem is: p(w_j) = c1*sin(2piw_jt)+c2*cos(2piw_jt).
The Least Squares solution to this is simply defined as:
c = inv(A'*A)*A'*l.
Where A is the first design matrix, which consists of partial derivatives of the
function model with respect to the unknowns, in this case they are c1 and c2.
l is the observation vector.
The coefficients are determined for every frequency spanning the time series.
So there are different coefficients c1 and c2 for every corresponding frequency w_j.
So the least squares solution has to be determined j times for each frequency.
Once all the functions are determined for every frequency spanning the time series.
They can be transformed into the frequency domain. The way this is done is by projecting
the vector of the newly determined p(w_j) onto the original time series. An orthogonal
projection of p onto l gives a measure of how much of p is contained in l. The better the
fit of p to the corresponding w, the higher the contribution is to the original time
series for that frequency.
The projection can be defined as s(w_j) = f'p/(f'f)
'''
from math import *
import numpy as np
import matplotlib.pylplot as plt
#test
t = np.linspace(0.001,0.119,100)
fq = np.linspace(1/max(t),1/min(t),100)
y1 = 9.8*np.cos(2*pi*200*t[:]+0)
y2 = 7.6*np.cos((2*pi*145*t[:])+(30*(pi/180)))
y3 = 5.4*np.cos((2*pi*93*t[:])+(70*(pi/180)))
y4 = 3.2*np.cos((2*pi*58*t[:])+(160*(pi/180)))
y5 = np.cos((2*pi*35*t[:])+(320*(pi/180)))
l = y1+y2+y3+y4+y5
print l