Generate manual repeatable signal for matlab

Dear matlab forum,
At this moment I'm evaluating a 'sample entropy' algorithm, that is used in heart and gait data currently.
I want to show the effect of sample frequency and time scales on the algorithms outcomes.
For this I want to create a manual function that can be repeated, that resembles the gait flexion angle of the knee.
This function must look like the negative part of a sinusoid (1Hz), amplitude -5, followed by the positive part of a sinusoid (2Hz) amplitude +1.
I should be able to use this function in a matlab script. At this point I'm quite a novice on this topic, and I want to see what your recommendations for designing such a function for matlab would be. I added a sketch to give a better idea on the curve of the function.
I look forward to seeing your answer(s).
Greetings,
Jeroen

 Accepted Answer

Like this?
dt = 0.01;
tf = 1.5;
n = tf/dt + 1;
for i = 1:n
t(i) = (i-1)*dt;
y(i) = wave(t(i));
end
plot(t,y),grid
function y = wave(t)
w1 = 2*pi; % frequency rads/sec
w2 = 4*pi; % "
A1 = -5; % amplitude
A2 = 1; % "
if t<1
y = A1*sin(w1*t);
else
y = A2*sin(w2*t);
end
end

1 Comment

Thanks very much Alan! For the ones intrested, I found one other possible method (upper). The bottom method is your method, slightly adjusted to make the signal look like a better flexion-extension angle of the knee joint.
a1 = 5; %amplitude
t1 = 0:dt:T1; %timevector
f = @(x) [ -5*sin(2*pi*0.8*x).*(0.005<=x & x<=0.6) + -sin(2*pi*2.5*x).*(0.6<x & x<0.8) + 0.8*sin(2*pi*1.25*x).*(0.8<x & x<1.2) + 0.*(1.2<=x & x<1.3) ];
x = linspace(0,1.3);
intvl = [0 6];
repfx = repmat(f(x), 1, diff(intvl)/3);
px = linspace(intvl(1),intvl(2),length(repfx));
figure(1)
plot(px, repfx)
grid
%% Try 2
dt = 0.01;
tf = 1.2;
n = tf/dt + 1;
for i = 1:n
t(i) = (i-1)*dt;
y(i) = wave(t(i));
end
plot(t,y),grid
function y = wave(t)
w1 = 2*pi; % frequency rads/sec
w2 = 6*pi; % "
A1 = -5; % amplitude
A2 = 1; % "
if t<=0.5
y = A1*sin(w1*t);
elseif t>0.5 && t<0.67
y = -A2*sin(w2*t);
elseif t>0.67 && t<1
y = 0.8*sin(3*pi*t)
else
y = 0
end
end

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Release

R2018a

Community Treasure Hunt

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

Start Hunting!