Error in integration of square wave using cumtrapz

Hi everyone,
I am currently plotting a square velocity profile for a moving platform of which I need to take the integral of to find the displacement. The code I have written lets the user input the time length of the waveform, the interval, velocity amplitude, and frequency and plots the corresponding square wave. Next, I use cumtrapz to calculate the integral of the square wave, which results in a triangular wave as expected.
After completing the integration, I center the triangular displacement around 0 by subtracting the mean to shift the waveform vertically, and then use a modified circshift (fshift from File Exchange) to horizontally shift the wave to it's closest y=0 value. This method seems to work well for certain frequency values (.6 to 1 Hz), however at lower frequency values (.1 to .5 Hz), the scale of the integration is incorrect. For a velocity of 15 cm/s, using a sine wave of or a square wave with frequency of 1 Hz, I calculate displacement values between 1.5 and 2 in. If I change the frequency to .5 Hz or less, the displacement then becomes between 2-3 in, which seems to be incorrect as amplitude of a function should not change with a frequency change.
I have been trying to figure out why at certain frequencies I am receiving this error, as in theory the frequency and amplitude of a function should be independent from one another. I would appreciate any help with figuring out this issue, thanks!
ft = 0:dt:time_length; %time vector definition
amp = 15; %cm/s velocity input
TV_ampin = amp/2.54; %cm/s to in/s
TV_freq = 1; %frequency in Hz
triV = TV_ampin.*square(2.*pi.*TV_freq.*ft,50); %create square velocity
triV_p = cumtrapz(ft, triV); %calculate integral
if triV_p(1,1) ~= 0 %if triV_p doesn't start at 0, shift it to zero by subtracting initial value
trivvy = triV_p - triV_p(1,1);
downshift = trivvy - mean(triV_p); %shift down by subtracting mean
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0);%define zero crosses function
zV = zci(downshift); %find zero crosses
triV_position = fshift(downshift, zV(1)); %horizontally shift by first zero cross
else %if it already starts at zero, do the same without subtracting initial
trivvy = triV_p;
downshift = trivvy - mean(triV_p);
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0);
zV = zci(downshift);
triV_position = fshift(downshift, zV(1));
end
figure; plot(ft, triV)
title('Velocity'); xlabel('Time (s)'); ylabel('Velocity');
figure; plot(ft, triV_position)
title('Position'); xlabel('Time (s)'); ylabel('Displacement');

6 Comments

hello
before jumping into code checking , I will come back on your assumptions :
If I change the frequency to .5 Hz or less, the displacement then becomes between 2-3 in, which seems to be incorrect as amplitude of a function should not change with a frequency change.
first , why do you speak of a function ? in my mind we are talking about amplitude (scalar) of velocity and displacement
maybe you are talking the integration function , which of course is frequency dependant
for a sine wave , amplitude of displacement D and velocity V are linked by V = D*(2*pi*freq), freq is frequency of input signal
so if you keep your input velocity constant at 15 cm/s and you halve the input frequency , D will double
there is no magic or error here
Yes I was mistaken, the frequency is definitely related to the integral of a function or dataset. When I plot a sine wave displacement for a specified time, frequency, and ampltidue, I can take the derivative of the function to get displacement:
sineVelocity = SV_ampin*sin(2*pi*SV_freq*ft+(pi/2));
sineV_position = -1*(SV_ampin/(2*pi*SV_freq))*cos(2*pi*SV_freq*ft+(pi/2));
Where I think there is an error is when I plot a square wave velocity and take the integral using cumtrapz, with the same time, frequency, and amplitude from the sine wave velocity, I get a much larger max displacement value for the triangular wave. I tried calculating the displacement from the sine vector using cumtrapz and it matches the sineV_position that is calculated above. If the frequency and amplitude of the sine and square waves match, should the amplitude of the integrals also match?
for me , integrating a signal is looking at the area under the curve
if I look at the area below the curve for a sine vs a square wave of same amplitude and frequency , the square wave area is only 1.5868 bigger than the sine wave area
this is the same factor you should see between displacement of sinus vs square velocity profile
vice versa !
this is the same factor you should see between displacement of square vs sinus velocity profile
Between velocity and displacement, yes there should be a common factor of difference. I figured that the amplitude of the integral of a sine wave and the amplitude of the integral of a square wave, with both original waves having the same amplitude, would also be equal. There are ways I can work around this however, as I have an amplitude limit at 2.5 inches. Thank you for your help!
you can also think of the square as a series of sine waves (fourier decomposition), so each sine after its own integration will add some extra amount of amplitude
so a sine and a square wave of same amplitude and freq will not have the same peak displacement after integration

Sign in to comment.

Answers (0)

Products

Release

R2020b

Asked:

on 15 Dec 2020

Commented:

on 17 Dec 2020

Community Treasure Hunt

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

Start Hunting!