Error in integration of square wave using cumtrapz

4 views (last 30 days)
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
Ethan
Ethan on 16 Dec 2020
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!
Mathieu NOE
Mathieu NOE on 17 Dec 2020
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

Community Treasure Hunt

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

Start Hunting!