用matlab画正弦平滑曲线而不是锯齿线

2019-04-15 13:58发布



%{ This is comment.
2016-07-17 Sunday, Im working at WanGen CDN inc. I saw a pic days ago in
the Enterprise QQ Group named CDN JiShu of the company, it is a pic of
a curve of a data sequence, I noticed that it is different to normal 
data curve for that it use sinusoidal curve instead of beeline, I feel
it is a creative method to let the curve look more smooth and less
sawtoothly.


This function receive two sequence as input arguments, ABSx is x axis,
ABSy is y asix.
function abPlotSin draws a semi cosine curve between two point, function
abFluentPlot call function abPlotSin to draws semi cosine curve between
every two neighbor points of the point sequence determined by ABSx and 
ABSy.

Usage example:     x = 1:10;     s = rand(1, 10);     abFluentPlot(x, s); %}


function abFluentPlot(ABSx, ABSy)
if length(ABSx) ~= length(ABSy)
    return;
end
if length(ABSx) == 1
    return;
end


%{ This is comment.
ABPlotX, ABPlotY, The x axis and y axis sequence used to draw
%}
ABPlotX = ABSx(1):(ABSx(end)-ABSx(1))/((length(ABSx)-1)*100):ABSx(end);
ABPlotY = ABPlotX;
for ABI = 1:(length(ABSx) - 1)
    ABPoint1 = [ABSx(ABI), ABSy(ABI)];
    ABPoint2 = [ABSx(ABI+1), ABSy(ABI+1)];
    
    [ABTmpX1, ABTmpY1] = abPlotSin(ABPoint1, ABPoint2);


    ABSubX1 = 1+(ABI-1)*100:1+ABI*100;
    ABPlotY(ABSubX1) = ABTmpY1;
end




plot(ABPlotX, ABPlotY, 'color', [0, 0, 0]);


hold on;
%{ This is comment.
The traditional beeline curve for compare with this smooth sinusoid curve
%}
plot(ABSx, ABSy, 'color', [0.5, 0.5, 0.5]);
hold off;


function [sx, sy] = abPlotSin(x1, x2)
%{ This is comment.
make sure that the x1 is at left and x2 at right.
%}
if x1(1) > x2(1)
    [x1(1), x2(1)] = abSwap(x1(1), x2(1));
    [x1(2), x2(2)] = abSwap(x1(2), x2(2));
end
widx = x2(1) - x1(1);
widy = abs(x2(2) - x1(2));
sx = x1(1):widx/100:x2(1);
if x1(2) > x2(2)
%{ This is comment
      widy         pi                       x1(2) - x2(2)
sy = ----- * cos(------ * (sx - x1(1))) +  -------------- + x2(2)
       2          widx                            2
%}
    sy = (widy/2)*cos((pi/widx) * (sx - x1(1))) + (x1(2)-x2(2))/2+x2(2);
else
%{ This is comment
      widy         pi                          x2(2) - x1(2)
sy = ----- * cos(----- * (sx - x1(1)) + pi) + --------------- + x1(2)
       2          widx                               2
%}
    sy = (widy/2)*cos((pi/widx) * (sx - x1(1)) + pi) + (x2(2)-x1(2))/2 + x1(2);
end
%{ This is unused code.
figure;
plot(sx, sy);
%}




function [x1, x2] = abSwap(x1, x2)
t = x1;
x1 = x2;
x2 = t;