Reputation: 539
In Matlab I am trying to achieve the following:
I have a data set that biologically represents activation in the brain when stimulus is given. So the data is such that we have 9 data points of stimulation, and then 15 of rest and it keeps going on like that for around 300 data points (4 mins in real-time).
I am able to plot the data easily but I am trying to overlay a square wave which represents the time in which there is "stimuluation" so that just by looking at the graph it is easy to see which is the rest period and which stimulation.
Very simply I have created a vector X and made it so that (the first 3 points are meant to be 0)
X = [0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0]
And plotted it on the same graph as the data. It works fine but the problem is that I need X to be created automatically according to my time scale.
Total_time = [-3:1.5:302]
This is my total time scale, from -3 to 302 seconds with the sampling rate as 1.5s. the first 3 points -3, -1.5, 0 are period of rest. Then from 0 seconds the stimulus starts for about 9 seconds (that will make 6 data points in that 9 second period).
So my question is - is it possible to use some sort of for loop to create this vector X to say that for 6 data points from 0 X = 1 and for the next 10 X = 0? I was thinking of the following:
X = zeros(1,304) %to create a 1x304 vector of zeros
X(0:3)=0
X(3:9)=1
X(9:19)=0
But then again.. this is writing it by hand.
Can anyone help?
Thanks!
Upvotes: 1
Views: 513
Reputation: 6084
As you already told us: What you have is a repeating pattern. In your case the pattern is the vector
pattern = [zeros(1,3) ones(1,9) zeros(1,15-3)];
So you could generate your signal by replicating this vector using repmat
:
startTime = -3;
endTime = 302;
timeStep = 1.5;
%%// Computation
time = startTime:timeStep:endTime;
numPatterns = ceil(length(time)/length(pattern));
X = repmat(pattern, 1, numPatterns);
%// As the pattern will end after endTime, we remove everything beyond endTime
X(length(time)+1:end) = [];
%%// Plot
plot(time, X);
Upvotes: 1
Reputation: 112669
This is easy with modulo operations:
m = 3; %// initial low period
n = 6; %// high period
p = 10; %// low period
s = 304; %// x size
x = [zeros(1,m) mod(0:s-m-1, n+p)<n];
Result (first values):
0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 ...
Note that n
and p
can be non-integer if needed. For example, if the high period lasts for 5 seconds and your sampling period is 1.5 seconds, just define n=5/1.5
, which gives
0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 ...
As can be seen, the high period lasts either 4 or 3 samples to accomodate the non-integer n
.
Upvotes: 1