%===========================================
%===== The Stretch And Squash Function =====
%===========================================

clear
clc

Amplitude = 100;
Frequency = 1; %Oscillations Per Second
Decay = 0.5; %Decay = 0 For No Decay
Phase = 0;
Duration = 5;
SamplingRate = 100;
AngularFrequency = 2*pi*Frequency;
Time = 0:1/SamplingRate:Duration;
PeriodCounter = 0;
QuarterPeriodCounter = 1;

X = (1:length(Time));
for i=2:length(Time)
display(i);

    StretchLevel(i) = Amplitude*sin(AngularFrequency*Time(i))./exp(Decay*Time(i));
    SquashLevel(i) = (1/X(i))*(Amplitude*Amplitude);
  
    if (rem(i,round(SamplingRate/(Frequency*2))) == 0)
        QuarterPeriodCounter = QuarterPeriodCounter + 1;
        if (rem(QuarterPeriodCounter,2) == 0)
            PeriodCounter = 1;
        else
            PeriodCounter = 0;
        end
    end
    
    figure(1);
    axis([-Amplitude Amplitude -Amplitude Amplitude]);
    title('The Stretch And Squash Function');
    xlabel('Time (Sec)');
    ylabel('Amplitude');
    grid on

    circle = rsmak('circle');
    BallShape = fncmb((circle),[StretchLevel(i)+SquashLevel(i) 0;0 StretchLevel(i)-SquashLevel(i)]);
    Radious = 1/sqrt(2);
    Ball = fncmb(fncmb(BallShape,[-Radious Radious;Radious Radious]),[0;0]);
    hold on
    
    if (PeriodCounter == 1)        
        fnplt(Ball,'r');
    else
        fnplt(Ball);
    end
   
    for ii=1:10000000
    end

    hold off
end