Smoothing a roughly sinusoidal signal

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Smoothing a roughly sinusoidal signal

llrjt100
Hi,

I have position data coming from a high resolution angular position sensor
and I need to improve the accuracy of the data by removing a regular
sinusoid from it.

The below graph shows three plots of angular position error against time:
<https://octave.1599824.n4.nabble.com/file/t373550/Forum.jpg>

1. The brown plot is the position error which needs to be corrected (ideal
is a zero error horizontal line).
2. The blue plot is the position error of a very high accuracy reference
angular position sensor (for testing only and not present in the deployed
application).
3. The green plot is the difference between the brown and blue plots.

Common to both the brown and blue plot is a fast in phase ~2 sec error which
can be ignored.
In the brown plot there's a ~24 sec error which I need to remove (the green
difference plot more clearly shows this as it removes the faster ~2 sec
error). The solution algorithm eventually needs to be deployed to run in
realtime on a 32 bit microcontroller.

The dataset is in .csv format which I can post for analysis.

I've Googled various examples which lead me to believe a solution to this
problem should be possible. The issue  is I don't have sufficient signal
processing experience to know how to implement a solution and would be very
grateful for any help in solving this problem.

Best wishes,
Richard




--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: Smoothing a roughly sinusoidal signal

GoSim
Reply | Threaded
Open this post in threaded view
|

RE: Smoothing a roughly sinusoidal signal

Windhorn, Allen E [ACIM/LSA/MKT]
In reply to this post by llrjt100
Richard,

> -----Original Message-----
> From: Help-octave <[hidden email]> On Behalf Of llrjt100
>
> I have position data coming from a high resolution angular position sensor
> and I need to improve the accuracy of the data by removing a regular
> sinusoid from it.
>
> The below graph shows three plots of angular position error against time:
> <https://octave.1599824.n4.nabble.com/file/t373550/Forum.jpg>
>
> 1. The brown plot is the position error which needs to be corrected (ideal
> is a zero error horizontal line).
> 2. The blue plot is the position error of a very high accuracy reference
> angular position sensor (for testing only and not present in the deployed
> application).
> 3. The green plot is the difference between the brown and blue plots.
>
> Common to both the brown and blue plot is a fast in phase ~2 sec error
> which can be ignored.
> In the brown plot there's a ~24 sec error which I need to remove (the green
> difference plot more clearly shows this as it removes the faster ~2 sec
> error).

The easy way would be to average it over 24 seconds.  This presumes that
the period of the unwanted signal remains close to 24 seconds.  This or any
other simple filter you adopt is going to introduce ~ 24 seconds delay in your
control loop.  Will that cause problems?

The unwanted oscillation doesn't appear to be actually sinusoidal, so parts
of it are going to get through your filter.  Maybe a simple low-pass Bessel
filter would be more effective.

Any way to measure or estimate the unwanted signal and subtract or cancel
it out?  Lyapunov observer?  Kalman filter?  Do you have a good model of the
internal system state?

> The dataset is in .csv format which I can post for analysis.

That would be interesting.

Regards,
Allen

Reply | Threaded
Open this post in threaded view
|

RE: Smoothing a roughly sinusoidal signal

llrjt100
Hi Allen,

Thanks for your reply.

For some reason my post appears twice in the topic list. I've uploaded the
.csv data in the first post.

The processed position data is the input for a motor controller, with a
control loop frequency of 20 Hz, so lag is undesirable. My first attempts at
this were with a simple moving average filter, from which I very quickly
learnt that the lag is proportional to how far back in time the filter
looks.

In terms of modelling the system, there's a stepper motor driving a gearbox,
the encoder is mounted on the output shaft. The fast ~2 s error in the graph
is microstep error, the ~24 s error in the graph is encoder interpolation
error, and most of the rest will be mechanical gearbox errors (the ratios of
each stage are known). The period of all the errors is related to the motor
velocity, so hopefully this constitutes a good model?



--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

RE: Smoothing a roughly sinusoidal signal

Windhorn, Allen E [ACIM/LSA/MKT]
> -----Original Message-----
> From: Help-octave <[hidden email]> On Behalf Of llrjt100
>
> The processed position data is the input for a motor controller, with a
> control loop frequency of 20 Hz, so lag is undesirable. My first attempts at
> this were with a simple moving average filter, from which I very quickly
> learnt that the lag is proportional to how far back in time the filter
> looks.

Oops, got this after I sent my last suggestion -- so moving average won't
work (too much delay).  So you want to filter out a signal that has a period
around 25 seconds, while preserving information that happens in 25 msec
(20 Hz loop rolloff), and without delaying the signals unduly.  If you can
accurately predict the "noise" signal in advance, then you can do this --
just subtract the known value of the noise from the desired signal.  If the
noise is unpredictable, then you won't be able to get good cancellation.
If you have a good enough model, it will predict the noise for you.

> In terms of modelling the system, there's a stepper motor driving a gearbox,
> the encoder is mounted on the output shaft. The fast ~2 s error in the graph
> is microstep error, the ~24 s error in the graph is encoder interpolation
> error, and most of the rest will be mechanical gearbox errors (the ratios of
> each stage are known). The period of all the errors is related to the motor
> velocity, so hopefully this constitutes a good model?

Can you formulate an equation or algorithm to describe it?  And what is
"encoder interpolation error"?  Is it predictable?

Why do you need an encoder?  A stepper motor always knows its own position
unless you overload it.  Of course there will be some slop in the gearing.

Regards,
Allen

Reply | Threaded
Open this post in threaded view
|

RE: Smoothing a roughly sinusoidal signal

llrjt100
Encoder interpolation error manifests as a regular periodic error in the
encoder position count when the encoder codewheel is rotating at constant
angular velocity. It's optically related to the number of lines on the
encoder codewheel (in this case 3600). In this dataset I've posted, the
codewheel, which is mounted on the gearbox output shaft, turns once every
86,164 seconds. This gives a encoder interpolation error period of 86,164
secs per rev / 3600 lines per rev = ~24 secs.

In terms of an algorithm/equation - the following periodic errors appear in
the encoder position data:

1. An encoder interpolation error sinusoid with period = secs per encoder
full rev / 3600 lines per rev. This has sub harmonics - the most significant
can be seen at approximately half this period.

2. A faster microstep error sinusoid with period approximately = 4x (secs
per encoder full rev / (gearbox ratio x 400))

All stepper motors have rotor positioning error - typically +/- 5%. Every
electrical cycle (4 full steps with a 2 phase stepper motor), this error
get's repeated. This can be seen as the ~2 s error in the graph I've posted.
Gearbox transmission error adds to this, so the resulting encoder position
is no longer directly correlated to the theoretical stepper motor position
(microstep count).

My gut feeling for the solution to this problem is to realtime fit a
sinusoid to the encoder position data, then use the sinusoid to correct the
current encoder position value. I'm not a signal processing engineer, so I
could well be completely wrong. I ran this by an academic friend earlier
this week, and they thought a Kalman filter would be the best solution. The
problem is I've no idea how to implement this...







--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: Smoothing a roughly sinusoidal signal

Nir Krakauer-3
If you already know the period of the sinusoidal error, you can use
linear regression to find the amplitude and phase of the sinusoid, by
fitting the parameters a and b in the function a*sin(kt) + b*cos(kt)
using least squares. You can fit multiple sine waves with different
periods as long as all those period lengths are known. If the error
characteristics are slowly changing, you could fit the sinusoid
function(s) using only the most recent few cycles of data, or weight
recent data more heavily (e.g. exponentially weighted linear
regression).


Reply | Threaded
Open this post in threaded view
|

Re: Smoothing a roughly sinusoidal signal

llrjt100
Thanks for your suggestion - I'd like to give this a try.

I'm very new to signal processing - how do I go about finding parameters a &
b?

I've adapted an example I found on the web, but not really sure what I'm
doing...

data = csvread('sidvel.csv');

t = data(:,1);
a = data(:,2);
s = data(:,3);
d = data(:,4);
plot(t,a,t,s,t,d);

%plot(abs(fft(a)));

%function with parameters
%fun = @(t,p) p(1)*sin(p(2)*t+p(3)) + p(4);  % sine wave with 4 parameters
to estimate
fun = @(t,p) p(1)*sin(p(2)*t) + p(3)*cos(p(2)*t); %a*sin(kt) + b*cos(kt)
fcn = @(p) sum((fun(t,p)-a).^2);  %Cost function to minimize the sum of the
squares

%Initial guess for parameters
p0 = [4 0 0 0];

%Parameter optimization
par = fminsearch(fcn, p0);

%See if estimated parameters match measured data
yest = fun(t, par)
plot(t,a,t,yest)




--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html