Bicha
Bicha

Reputation: 43

How to generate data with a specific trend in MATLAB

I want to test the Akaike criterion (it is a criterion that gives where do you get a significant change in a time series), but to do that I need to generate data that for example follow a sinusoidal trend, a linear trend with positive or negative slope, a constant trend, etc. So far I have done this but with random numbers, this is:

%Implementation of the Akaike method for Earth sciences.

N=100;
data=zeros(N,1);

for i=1:N
    data(i,1)=unifrnd(1,N);
end

%AIC=zeros(N-1,1);
data=rand(1,N);
for k=1:N
    %y=datasample(data,k);
    AIC(k,1)=k*log(var(data(1:k),1))+(N-k-1)*log(var(data(k+1:N),1));

end
AIC(1)=NaN;
%AIC(N-1)=[];AIC(N)=[];
%disp(AIC)
%plot(AIC)
subplot(2,1,1)
plot(data,'Marker','.')
subplot(2,1,2)
plot(AIC,'Marker','.')

So, How can I generate different data with different trend in MATLAB?

Thanks a lot in advance.

Upvotes: 0

Views: 686

Answers (1)

rayryeng
rayryeng

Reputation: 104514

What you can do is first start off with a known curve, then add some noise or random values so that the signal does follow a trend but it is noisy. Given a set of independent values, use these to generate values for your sinusoidal curves, a line with a positive or negative slope and a constant value.

Something like this comes to mind:

X = 1 : N; % N is defined in your code
Y1 = sin(X) + rand(1, N); % Sinusoidal

slope1 = 2; intercept = 3;
Y2 = slope1*X + intercept + rand(1, N);  % Line with a positive slope

slope2 = -1; intercept2 = 0.5;
Y3 = slope2*X + intercept2 + rand(1, N); % Line with a negative slope

B = 2;
Y4 = B*ones(1, N) + rand(1, N); % Constant line 

rand is a function in MATLAB that uniformly generates floating-point values between [0,1]. Y1, Y2, Y3 and Y4 are the trends you desire where they follow the curve defined but they add a bit of random values so that you don't completely get the trend you want and the noise is designed to decrease how similarity those curves are to the curve you defined. Increase the magnitude of the random values to decrease the similarity.

Upvotes: 1

Related Questions