Sine wave frequency estimation with scipy.least_squares on Python












0















I am trying to estimate the sine wave frequency using scipy.least_squares with Python. I cannot understand why it does not work.
My code is:



import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import least_squares

def generate_data(t, A0, A, omega, phase):
y = A0 + A * np.sin(omega * t + phase)
return y


# Init params
A0 = 0
A = 7.744444
omega = 2*np.pi*1 #2*np.pi*0.2
phase = np.pi/2
t_min = 0
t_max = 100
# Model of sine
t_model = np.linspace(t_min, t_max, 100)
y_model = generate_data(t_model, A0= A0, A=A, omega=omega, phase=phase)
plt.plot(t_model,y_model)

# Target func
def fun(x, t, y):
return (x[0]*np.sin(x[1] * t + x[2])) - y


x0 = np.ones(3)
res_lsq = least_squares(fun, x0, args=(t_model, y_model))

res_robust = least_squares(fun, x0, loss='soft_l1', f_scale=0.1, args=(t_model, y_model))
print('res_robust.x,',res_robust.x[1], 'expect:',omega)
print('res_lsq.x = ', res_lsq.x[1], 'expect:',omega)









share|improve this question




















  • 2





    Can you elaborate "it dosn't work"?

    – bipll
    Nov 21 '18 at 7:06











  • Frequency and other parameters are estimated incorrectly

    – Dimitros MDA
    Nov 21 '18 at 7:08








  • 1





    Please also include all your imports so that one can easily copy&paste code. Please also elaborate on why you think that the parameters are estimated incorrectly (expected vs. actual outcome).

    – Cleb
    Nov 21 '18 at 7:27











  • Your initial estimate is quite far away from the true parameters. Do things improve if you give a better estimate? Moreover you look to be sampling once a second, yet wanting to estimate a 7Hz signal. You should consider nyquist

    – dmuir
    Nov 21 '18 at 10:55













  • Many thanks to all for answers! I found the bug) Problem here: t_max = 100 )) Of course it should be 1..

    – Dimitros MDA
    Nov 21 '18 at 10:58


















0















I am trying to estimate the sine wave frequency using scipy.least_squares with Python. I cannot understand why it does not work.
My code is:



import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import least_squares

def generate_data(t, A0, A, omega, phase):
y = A0 + A * np.sin(omega * t + phase)
return y


# Init params
A0 = 0
A = 7.744444
omega = 2*np.pi*1 #2*np.pi*0.2
phase = np.pi/2
t_min = 0
t_max = 100
# Model of sine
t_model = np.linspace(t_min, t_max, 100)
y_model = generate_data(t_model, A0= A0, A=A, omega=omega, phase=phase)
plt.plot(t_model,y_model)

# Target func
def fun(x, t, y):
return (x[0]*np.sin(x[1] * t + x[2])) - y


x0 = np.ones(3)
res_lsq = least_squares(fun, x0, args=(t_model, y_model))

res_robust = least_squares(fun, x0, loss='soft_l1', f_scale=0.1, args=(t_model, y_model))
print('res_robust.x,',res_robust.x[1], 'expect:',omega)
print('res_lsq.x = ', res_lsq.x[1], 'expect:',omega)









share|improve this question




















  • 2





    Can you elaborate "it dosn't work"?

    – bipll
    Nov 21 '18 at 7:06











  • Frequency and other parameters are estimated incorrectly

    – Dimitros MDA
    Nov 21 '18 at 7:08








  • 1





    Please also include all your imports so that one can easily copy&paste code. Please also elaborate on why you think that the parameters are estimated incorrectly (expected vs. actual outcome).

    – Cleb
    Nov 21 '18 at 7:27











  • Your initial estimate is quite far away from the true parameters. Do things improve if you give a better estimate? Moreover you look to be sampling once a second, yet wanting to estimate a 7Hz signal. You should consider nyquist

    – dmuir
    Nov 21 '18 at 10:55













  • Many thanks to all for answers! I found the bug) Problem here: t_max = 100 )) Of course it should be 1..

    – Dimitros MDA
    Nov 21 '18 at 10:58
















0












0








0








I am trying to estimate the sine wave frequency using scipy.least_squares with Python. I cannot understand why it does not work.
My code is:



import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import least_squares

def generate_data(t, A0, A, omega, phase):
y = A0 + A * np.sin(omega * t + phase)
return y


# Init params
A0 = 0
A = 7.744444
omega = 2*np.pi*1 #2*np.pi*0.2
phase = np.pi/2
t_min = 0
t_max = 100
# Model of sine
t_model = np.linspace(t_min, t_max, 100)
y_model = generate_data(t_model, A0= A0, A=A, omega=omega, phase=phase)
plt.plot(t_model,y_model)

# Target func
def fun(x, t, y):
return (x[0]*np.sin(x[1] * t + x[2])) - y


x0 = np.ones(3)
res_lsq = least_squares(fun, x0, args=(t_model, y_model))

res_robust = least_squares(fun, x0, loss='soft_l1', f_scale=0.1, args=(t_model, y_model))
print('res_robust.x,',res_robust.x[1], 'expect:',omega)
print('res_lsq.x = ', res_lsq.x[1], 'expect:',omega)









share|improve this question
















I am trying to estimate the sine wave frequency using scipy.least_squares with Python. I cannot understand why it does not work.
My code is:



import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import least_squares

def generate_data(t, A0, A, omega, phase):
y = A0 + A * np.sin(omega * t + phase)
return y


# Init params
A0 = 0
A = 7.744444
omega = 2*np.pi*1 #2*np.pi*0.2
phase = np.pi/2
t_min = 0
t_max = 100
# Model of sine
t_model = np.linspace(t_min, t_max, 100)
y_model = generate_data(t_model, A0= A0, A=A, omega=omega, phase=phase)
plt.plot(t_model,y_model)

# Target func
def fun(x, t, y):
return (x[0]*np.sin(x[1] * t + x[2])) - y


x0 = np.ones(3)
res_lsq = least_squares(fun, x0, args=(t_model, y_model))

res_robust = least_squares(fun, x0, loss='soft_l1', f_scale=0.1, args=(t_model, y_model))
print('res_robust.x,',res_robust.x[1], 'expect:',omega)
print('res_lsq.x = ', res_lsq.x[1], 'expect:',omega)






python scipy least-squares non-linear-regression






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 20:26









Nic3500

3,35081829




3,35081829










asked Nov 21 '18 at 7:01









Dimitros MDADimitros MDA

44




44








  • 2





    Can you elaborate "it dosn't work"?

    – bipll
    Nov 21 '18 at 7:06











  • Frequency and other parameters are estimated incorrectly

    – Dimitros MDA
    Nov 21 '18 at 7:08








  • 1





    Please also include all your imports so that one can easily copy&paste code. Please also elaborate on why you think that the parameters are estimated incorrectly (expected vs. actual outcome).

    – Cleb
    Nov 21 '18 at 7:27











  • Your initial estimate is quite far away from the true parameters. Do things improve if you give a better estimate? Moreover you look to be sampling once a second, yet wanting to estimate a 7Hz signal. You should consider nyquist

    – dmuir
    Nov 21 '18 at 10:55













  • Many thanks to all for answers! I found the bug) Problem here: t_max = 100 )) Of course it should be 1..

    – Dimitros MDA
    Nov 21 '18 at 10:58
















  • 2





    Can you elaborate "it dosn't work"?

    – bipll
    Nov 21 '18 at 7:06











  • Frequency and other parameters are estimated incorrectly

    – Dimitros MDA
    Nov 21 '18 at 7:08








  • 1





    Please also include all your imports so that one can easily copy&paste code. Please also elaborate on why you think that the parameters are estimated incorrectly (expected vs. actual outcome).

    – Cleb
    Nov 21 '18 at 7:27











  • Your initial estimate is quite far away from the true parameters. Do things improve if you give a better estimate? Moreover you look to be sampling once a second, yet wanting to estimate a 7Hz signal. You should consider nyquist

    – dmuir
    Nov 21 '18 at 10:55













  • Many thanks to all for answers! I found the bug) Problem here: t_max = 100 )) Of course it should be 1..

    – Dimitros MDA
    Nov 21 '18 at 10:58










2




2





Can you elaborate "it dosn't work"?

– bipll
Nov 21 '18 at 7:06





Can you elaborate "it dosn't work"?

– bipll
Nov 21 '18 at 7:06













Frequency and other parameters are estimated incorrectly

– Dimitros MDA
Nov 21 '18 at 7:08







Frequency and other parameters are estimated incorrectly

– Dimitros MDA
Nov 21 '18 at 7:08






1




1





Please also include all your imports so that one can easily copy&paste code. Please also elaborate on why you think that the parameters are estimated incorrectly (expected vs. actual outcome).

– Cleb
Nov 21 '18 at 7:27





Please also include all your imports so that one can easily copy&paste code. Please also elaborate on why you think that the parameters are estimated incorrectly (expected vs. actual outcome).

– Cleb
Nov 21 '18 at 7:27













Your initial estimate is quite far away from the true parameters. Do things improve if you give a better estimate? Moreover you look to be sampling once a second, yet wanting to estimate a 7Hz signal. You should consider nyquist

– dmuir
Nov 21 '18 at 10:55







Your initial estimate is quite far away from the true parameters. Do things improve if you give a better estimate? Moreover you look to be sampling once a second, yet wanting to estimate a 7Hz signal. You should consider nyquist

– dmuir
Nov 21 '18 at 10:55















Many thanks to all for answers! I found the bug) Problem here: t_max = 100 )) Of course it should be 1..

– Dimitros MDA
Nov 21 '18 at 10:58







Many thanks to all for answers! I found the bug) Problem here: t_max = 100 )) Of course it should be 1..

– Dimitros MDA
Nov 21 '18 at 10:58














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406790%2fsine-wave-frequency-estimation-with-scipy-least-squares-on-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53406790%2fsine-wave-frequency-estimation-with-scipy-least-squares-on-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?