To the purpose of fitting a 2nd order polynomial (a + b*x + c*x^2)  to the 
chunk of signal falling in a 17 consecutive samples window
I wrote the following very crude script. Since I have no previous experience of 
using Least Square Fit with R I would appreciate 
your supervision and suggestion.
I guess the returned coefficients of the oolynomial are: 
a = -1.3191398 
b = 0.1233055 
c = 0.9297401 


Thank you very much in advance,
Maura

######################################################
## Main

tms <- t(read.table("signal877cycle1.txt"))
J <- ilogb(length(tms), base=2) + 1
y <- c(tms,rep(0,2^J - length(tms)))
y.win <- tms.ext[1:17]
ls.mat <- matrix(nrow=length(y.win),ncol=3,byrow=TRUE)
dt <- 0.033
ls.mat[,1] <- 1
ls.mat[,2] <- seq(0,dt*(length(y.win)-1),dt)
ls.mat[,3] <- ls.mat[,2]^2
#############################################################

> tms <- t(read.table("signal877cycle1.txt"))
> J <- ilogb(length(tms), base=2) + 1
> y <- c(tms,rep(0,2^J - length(tms)))
> y.win <- tms.ext[1:17]
> ls.mat <- matrix(nrow=length(y.win),ncol=3,byrow=TRUE)
> dt <- 0.033
> ls.mat[,1] <- 1
> ls.mat[,2] <- seq(0,dt*(length(y.win)-1),dt)
> ls.mat[,3] <- ls.mat[,2]^2
> y
  [1] -1.29882462 -1.29816465 -1.29175902 -1.33508315 -1.31905086 -1.30246447 
-1.25496640 -1.25858566 -1.19862868
 [10] -1.16985809 -1.15755035 -1.15627040 -1.10929231 -1.09324296 -1.07202676 
-1.03543530 -1.00609649 -0.96931799
 [19] -0.96014189 -0.93879923 -0.89472101 -0.86568807 -0.86394226 -0.83804684 
-0.79226517 -0.74804696 -0.69506558
 [28] -0.63984135 -0.57677266 -0.52376371 -0.48793752 -0.44261935 -0.37505621 
-0.30538492 -0.19309771 -0.07859412
 [37] -0.01879655  0.04247391  0.09565881  0.17329566  0.29132263  0.38380712  
0.45016443  0.50107765  0.57413940
 [46]  0.68835476  0.78369090  0.83756871  0.87753415  0.92834503  0.99560230  
1.08055356  1.17121517  1.22967280
 [55]  1.25791166  1.28749046  1.31672692  1.33188866  1.35420775  1.37356226  
1.38792638  1.40398573  1.41558702
 [64]  1.39204622  1.39848595  1.39902593  1.40604565  1.42092504  1.41436531  
1.38666643  1.36012986  1.32950875
 [73]  1.26507137  1.25315597  1.18249472  1.08857029  0.98782261  0.90470599  
0.83081192  0.77709116  0.65228917
 [82]  0.51844166  0.44530462  0.39562664  0.30153281  0.17979539  0.09895985  
0.04306094 -0.03937571 -0.14150334
 [91] -0.25936679 -0.31480454 -0.38806157 -0.47389691 -0.50785671 -0.58179371 
-0.67538285 -0.74246719 -0.78380551
[100] -0.83894328 -0.86450224 -0.90614055 -0.93751928 -0.99679687 -1.03205956 
-1.06616465 -1.06651404 -1.14997066
[109] -1.18338930 -1.21335809 -1.20208854 -1.22370767 -1.23488486 -1.25112655 
-1.26942581 -1.26792234 -1.28838504
[118] -1.28799329 -1.27326566 -1.28502518  0.00000000  0.00000000  0.00000000  
0.00000000  0.00000000  0.00000000
[127]  0.00000000  0.00000000
> y.win
 [1] -1.298825 -1.298165 -1.291759 -1.335083 -1.319051 -1.302464 -1.254966 
-1.258586 -1.198629 -1.169858 -1.157550
[12] -1.156270 -1.109292 -1.093243 -1.072027 -1.035435 -1.006096
> ls.mat
      [,1]  [,2]     [,3]
 [1,]    1 0.000 0.000000
 [2,]    1 0.033 0.001089
 [3,]    1 0.066 0.004356
 [4,]    1 0.099 0.009801
 [5,]    1 0.132 0.017424
 [6,]    1 0.165 0.027225
 [7,]    1 0.198 0.039204
 [8,]    1 0.231 0.053361
 [9,]    1 0.264 0.069696
[10,]    1 0.297 0.088209
[11,]    1 0.330 0.108900
[12,]    1 0.363 0.131769
[13,]    1 0.396 0.156816
[14,]    1 0.429 0.184041
[15,]    1 0.462 0.213444
[16,]    1 0.495 0.245025
[17,]    1 0.528 0.278784
> lsfit(x, y, wt = NULL, intercept = TRUE, tolerance = 1e-07,

+            yname = NULL> 
> lsfit(ls.mat, y.win,wt = NULL, intercept = TRUE, tolerance = 1e-07,yname = 
> NULL)
$coefficients
 Intercept         X1         X2         X3 
-1.3191398  0.1233055  0.9297401  0.0000000 

$residuals
 [1]  0.020315146  0.015893550  0.015192628 -0.037263015 -0.032387216 
-0.028982296  0.003309337 -0.017541342
 [9]  0.023159250  0.030648485  0.019649885 -0.004401476  0.015220334  
0.001888425 -0.008301609 -0.005141358
[17] -0.011258729

$intercept
[1] TRUE

$qr
$qt
 [1]  4.937370523  0.409411205 -0.089144866 -0.041892736 -0.035696706 
-0.031176843  0.002024443 -0.018121872
 [9]  0.023077794  0.030860815  0.019950712 -0.004217443  0.015082286  
0.001223006 -0.009699688 -0.007477386
[17] -0.014737995

$qr
       Intercept          X2          X3            X1
 [1,] -4.1231056 -1.08849989 -0.39512546 -4.123106e+00
 [2,]  0.2425356  0.66656733  0.35194755  1.558035e-17
 [3,]  0.2425356  0.21973588 -0.09588149  1.787189e-17
 [4,]  0.2425356  0.17022850 -0.10350966 -2.990539e-17
 [5,]  0.2425356  0.12072112 -0.19811319  2.906411e-01
 [6,]  0.2425356  0.07121375 -0.27000118  2.654896e-01
 [7,]  0.2425356  0.02170637 -0.31917362  2.457966e-01
 [8,]  0.2425356 -0.02780101 -0.34563052  2.315620e-01
 [9,]  0.2425356 -0.07730838 -0.34937188  2.227859e-01
[10,]  0.2425356 -0.12681576 -0.33039769  2.194681e-01
[11,]  0.2425356 -0.17632314 -0.28870796  2.216089e-01
[12,]  0.2425356 -0.22583052 -0.22430269  2.292080e-01
[13,]  0.2425356 -0.27533789 -0.13718188  2.422656e-01
[14,]  0.2425356 -0.32484527 -0.02734553  2.607816e-01
[15,]  0.2425356 -0.37435265  0.10520637  2.847560e-01
[16,]  0.2425356 -0.42386002  0.26047381  3.141889e-01
[17,]  0.2425356 -0.47336740  0.43845680  3.490802e-01

$qraux
[1] 1.242536 1.269243 1.013809 1.321251

$rank
[1] 3

$pivot
[1] 1 3 4 2

$tol
[1] 1e-07

attr(,"class")
[1] "qr"

Warning message:
In lsfit(ls.mat, y.win, wt = NULL, intercept = TRUE, tolerance = 1e-07,  :
  'X' matrix was collinear




tutti i telefonini TIM!


        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to