repeating step signal in real time with a hold value of 5 seconds

2 views (last 30 days)
Ramón Tamino Uhl
Ramón Tamino Uhl on 6 Jul 2021
Answered: Peter Perkins on 28 Jul 2021
Hello dear community,
I am working on my Bachelor thesis and have encountered a problem which I could not solve with different methods.
I am building a Robot with a LEGO EV3 Brick and Motor. For modeling purposes I need to gain experimental data to build a function that translates my input u (torque) into power as the Simulink encoder for LEGO works with power values from -100 to 100.
To do so I build an experiment with a digital clock that counts from -100 to 100 and I gave this signal into the motor. I saved the data from the encoder and used polyfit to approximate the data. This worked, but there is a big margin of error.
So I want to build a repeating step signal, that holds the power level for 5 seconds and then makes a step of 1 to the next power level. This way I could calculate the average value over 5 seconds. But I have not been successful in generating a signal like this.
I would appreciate any kind of help.
The signal would look something like this:
_ _ _ _ _| step of 1
_ _ _ _ _|
_ _ _ _ _|
5 seconds
Kind regards,

Answers (2)

Mathieu NOE
Mathieu NOE on 7 Jul 2021
my 2 cents suggestion... adapt sampling frequency to your own needs
Fs = 100;
dt = 1/Fs;
duration = 5 ;
nb_steps = 10
samples = Fs*duration;
y = ones(samples,1);
out = y;
for ci = 2:nb_steps
out = [out; ci*y];
plot(out) % time axis in samples

Peter Perkins
Peter Perkins on 28 Jul 2021
Like this?
>> interp1([1 5 6 10 11 15],[1 1 2 2 3 3],1:15,"prev")
ans =
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3
I mean, a signal all by itself seems unusual, so likely if there was a bigger picture I'd recommend using a timetable and retime to do that "prev" interpolation.

Community Treasure Hunt

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

Start Hunting!

Translated by