TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

How to Linear-Fit a Noisy Signal with Regular Discontinuities

41 pointsby jgforbesover 6 years ago

2 comments

joferover 6 years ago
This particular problem pops up in quite a few domains. (We often refer to it as &quot;phase unwrapping&quot; in the geosciences.) The approach here is a good one so long as your noise doesn&#x27;t result in lots of mistaken &quot;wrap-arounds&quot;.<p>However, it will fail badly in the presence of noise in many cases. It&#x27;s particularly problematic when the slope changes (e.g. a polynomial) or where the slope is high and the noise is high. (Note that polynomials are still linear in the sense mentioned here: linear regression).<p>At any rate, this is definitely a nice write-up, but a bit more discussion of where the approach breaks down would be useful. It&#x27;s actually a classic example of an elegant solution that breaks down frequently in practice (i.e. it&#x27;s commonly used as a teaching example in various courses). A better solution is usually more complex, domain specific, and therefore out-of-scope, but failure modes for this method make for a nice set of examples.
评论 #18811277 未加载
评论 #18810631 未加载
GChevalierover 6 years ago
I would have instead done like this:<p>(tldr; use a sine and cosine function regression like a linear regression. Think like solving for a free angle and a free phase instead than for a free bias and weight).<p>1. Convert the hours to an angle in degrees or in radians (a simple linear transformation).<p>2. Take the cos and sin of the angle to get the x and y position in a plane, respectively.<p>3. Introduce a time axis such that the thing doesn&#x27;t draw a circle but rather an helix (like DNA).<p>4. So we now have a ton of 3D data points: (time, x, y). Create a ML model to fit a sine and a cosine to those data points to match them perfectly. Your model has only 2 free parameters to optimize for: a shared phase offset and a shared frequency. The sine uses (time, y) and the cosine (time, x).<p>5. Initialize the model with a random phase offset and a frequency ideally already close to the one you think you have. Don&#x27;t initialize with a too high frequency to avoid fitting just Nyquist-frequency-close-noise.<p>6. Optimize! (With the least squares.) I guess that you might congerge only to a local minima and need to try different randon starting frequencies if you fail to converge.<p>7. The answer to your problem is the now-optimized free parameter of the frequency. It won&#x27;t sit between two bins of your fft anymore.<p>Related: <a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;16716302&#x2F;how-do-i-fit-a-sine-curve-to-my-data-with-pylab-and-numpy" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;16716302&#x2F;how-do-i-fit-a-...</a><p>Note: This link contains images picturing the transformations I try to explain.<p>Disclaimer: I didn&#x27;t do that yet, this is just off the top of my head. If I said something wrong, please comment. Mostly about a wrong convergence to Nyquist freq or something like that (?).<p>In the end, this way, you won&#x27;t have discrete fft bins. You&#x27;ll approach the problem orthogonally to that: you solve for finding the one best fft bin (frequency) directly.<p>In other words: solve for the content in the exponential of &quot;e&quot; as free parameters, and for one such frequency and phase offset instead of many bins.
评论 #18810599 未加载