Offset removal from a sinusoidal wave

Hello Everyone,
I have an input waveform as shown in the figure in blue. It is a sinusoidal wave but due to different offests during positive and negative half cycles, it looks distorted. Basically, there is a big jump whenever signal changes direction from positive to negative and vice versa. So, I want to remove offsets from individual half cycles. I want an output which looks as red in the figure, which is just shifting of top and bottom parts of the waveform, taking zero as the baseline, excluding the big jumps. I have attached the input csv file as well. If anyone has any idea, how to approach it, please let me know, I will be thankful.

 Accepted Answer

Try this:
D1 = readmatrix('Output.csv');
x = 1:numel(D1);
D1HL = min(D1(D1>0));
D1HI = (D1>D1HL); % Logical Index
D1LL = max(D1(D1<0));
D1LI = (D1<D1LL); % Logical Index
minH = zeros(size(D1))+min(D1(D1HI));
maxL = zeros(size(D1))+max(D1(D1LI));
D1C = zeros(size(D1));
D1C(D1HI) = D1(D1HI)-minH(D1HI);
D1C(D1LI) = D1(D1LI)-maxL(D1LI);
figure
plot(x, D1C)
grid
xlim([0 100]) % Delete Or Change To See Larger (Or Entire) Vector
producing:
I plotted the points as well as the connecting lines to demonstrate the continuity. Change the line style to '-' to plot it without the points.
.

4 Comments

Thank you very much for your response Star Strider!
While it is working fine for the dataset which I attached here, but when I try to use the full dataset which is a bit longer in terms of number of samples (around 16000 samples), I do not get the required output. I have attached here the full dataset.
Thanks
Your data has outliers, so it is necessary to remove them first.
Try this:
D1 = readmatrix('OutputFull.csv');
x = 1:numel(D1);
D1HI = (D1>0); % Logical Index
D1H = D1(D1HI);
D1(D1HI) = filloutliers(D1H,'linear'); % Detect & Linearly Interpolate Outliers
D1HL = min(D1(D1HI))
D1LI = (D1<0); % Logical Index
D1L = D1(D1LI);
D1(D1LI) = filloutliers(D1L,'linear'); % Detect & Linearly Interpolate Outliers
D1LL = max(D1(D1LI));
minH = zeros(size(D1))+min(D1(D1HI));
maxL = zeros(size(D1))+max(D1(D1LI));
D1C = zeros(size(D1));
D1C(D1HI) = D1(D1HI)-minH(D1HI);
D1C(D1LI) = D1(D1LI)-maxL(D1LI);
figure
plot(x, D1C, '.-')
grid
xlim([0 100]) % Delete Or Change To See Larger (Or Entire) Vector
producing:
Other than removing the outliers (and sllight changes needed to reflect that), the code is essentially the same as previously. This code should be robust to other such data sets.
Thank you very much Star Strider. That works perfectly.
As always, my pleasure!

Sign in to comment.

More Answers (1)

I have an idea
clc,clear
x = linspace(0,20,100);
y = sin(x);
ind = -0.5<y & y<0.5;
x1 = x(~ind);
y1 = y(~ind);
y1 = y1 - sign(y1)*0.3;
plot(x,y)
hold on
plot(x1,y1,'.r')
hold off

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!