Generate unevenly-spaced synthetic time series

11 views (last 30 days)
Vince Clementi
Vince Clementi on 26 Apr 2019
Edited: Adam Danz on 26 Apr 2019
I have created a synthetic time series (x) over time (tsyn), but I need to remake this so that the spacing between points in tsyn is both uneven and random between 1 and 5.
tsyn = 1:1600;
x = 2*sin(2*pi*tsyn/100)+1*sin(2*pi*tsyn/41)+0.5*sin(2*pi*tsyn/21);
I thought it would be something like the below code, but that didn't work.
tsyn = 1:randn(1:5):1600;
Any advice?
Adam Danz
Adam Danz on 26 Apr 2019
Ah, I just saw this. I updated my solution to cover both possibilities.

Sign in to comment.

Answers (2)

Adam Danz
Adam Danz on 26 Apr 2019
Edited: Adam Danz on 26 Apr 2019
If your samples are not integers
You can set the maximum sample, the [min, max] of your bounds, and the starting number. tsyn is a monotonically increasing vector with random intervals from a bounded, uniform distribution starting at your specified starting value and ending at or just before your maximum sample.
maxSample = 1600; %maximum value allowed
bounds = [1,5]; %bounds to the uniformly distribution random intervals between samples (inclusive)
startNum = 1; %first sample (starting point)
% Create samples at random intervals
minSamples = floor(maxSample / bounds(1)); %you'll end up with at least this many samples
randIntervals = (rand(minSamples,1) * range(bounds)) + bounds(1);
tsyn = [startNum; cumsum(randIntervals)+startNum];
tsyn(tsyn > maxSample) = []; %get rid of extra values
histogram(diff(tsyn), bounds(1):bounds(2))
set(gca, 'Xtick', bounds(1):bounds(2))
If your samples should be intergers
Replace the "randIntervals =" line above with the one below
randIntervals = randi(range(bounds)+1, minSamples, 1)+bounds(1)-1;
And if you need convinced that the intervals are randomly distributed between your bounds:
histogram(diff(tsyn), bounds(1):bounds(2)+1)
set(gca, 'Xtick', bounds(1):bounds(2))

John D'Errico
John D'Errico on 26 Apr 2019
Edited: John D'Errico on 26 Apr 2019
It failed, because the colon operator does not accept a random increment. Anyway, randn(1:5) does not generate a random number between 1 and 5 anyway. I'd suggest you need to read the getting started tutorials.
Doing what you have asked is not trivial however. One approach would use Poisson arrivals. But I recall that would imply an exponential interarrival time. And you are asking for a uniformly distributed interarrival time.
Simplest would be to use Roger Stafford's randfixedsum, available for free download from the File Exchange.
If the spacing is to be from 1 to 5, then the average spacing is 3. Over the interval [1,1600], that means you would expect
ans =
So you want to find a set of 533 random numbers, uniformly distributed on the interval [1,5] that sum to 1599.
delta = randfixedsum(533,1,1599,1,5)';
tsyn = cumsum([1,delta]);
As you can see, this set has the desired properties. It runs from 1 to 1600.
ans =
ans =
As well, it is uniformly distributed in terms of the inter-arrivals. (Well approximately so. With a larger sample size, it will be closer to uniform.)
ans =
ans =
You can find randfixedsum here:

Community Treasure Hunt

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

Start Hunting!

Translated by