📄 xrwalk.m
字号:
% xrwalk.m
% Scope: This MATLAB program generates a random walk process, and plots the
% random walk sequence and the normalized auto-correlation sequence
% (optional).
% Usage: xrwalk
% Inputs: - number of random number to be generated
% - value of the noise covariance, q*dt
% - value of the initial seed, e.g. 0
% - name of the output file (optional), if the generated data are saved
% Outputs: - plot of the random walk sequence
% - plot of the normalized auto-correlation sequence
% External Matlab macros used: genrn, rms, rwalk, xcorr (from Signal
% Processing Toolbox) - optional
% Last update: 06/27/00
% Copyright (C) 1996-00 by LL Consulting. All Rights Reserved.
clear
yes = 'y';
answer1 = yes;
disp(' ');
while (strcmp(answer1,yes) == 1)
clear x
close all
% Specify the input data
nstep = input('Specify the number of random numbers to be generated --> ');
disp(' ');
qdt = input('Specify the value of the noise covariance, q*dt --> ');
disp(' ');
iseed = input('Specify the value of the initial seed, iseed, e.g. 0 --> ');
disp(' ');
% Generate the random walk process (zero mean and standard deviation equals
% to q*dt)
x = rwalk(nstep,qdt,iseed);
% Determine mean, standard deviation and root mean square (rms) for the
% generated random numbers
xmean = mean(x);
xstd = std(x);
xrms = rms(x);
temp1 = ['mean = ',num2str(xmean)];
temp2 = ['st.dev. = ',num2str(xstd)];
temp3 = ['rms = ',num2str(xrms)];
g = [temp1,' ; ',temp2,' ; ',temp3];
% Save the generated data into a specified file if desired
answer2 = input('Do you want to save the generated data? (y/n)[y] --> ','s');
if isempty(answer2)
answer2 = 'y';
end
if strcmp(answer2,yes) == 1
disp(' ');
f2 = input('Specify the output file name --> ','s');
for k = 1:nstep
fprintf(f2,'%24.14e\n',x(k));
end
end
% Plot the random walk sequence
dd = num2str(nstep);
aa = 'Random walk process - with ';
aa = [aa dd ' time sequences'];
ee = 'Random walk sequence';
ff = 'Time sequence';
disp(' ');
disp('Execute random walk graph ... ');
disp('Select the mouse position to insert statistics text on the graph...');
temp = input('Press a key to start ...');
figure(1)
plot(x),...
title(aa), ylabel(ee), xlabel(ff), grid,...
gtext(g); % mouse placement of the text on a graph
disp(' ');
disp('Check the window containing the Random Walk Graph and . . .');
disp('Press a key to continue . . . ');
pause
% Plot the normalized auto-correlation sequence
disp(' ');
disp('For the auto-correlation sequence - the function XCORR from ');
disp('Signal Processing Toolbox is required');
answer3 = input('Plot the auto-correlation sequence? (y/n)[y] --> ','s');
if isempty(answer3)
answer3 = 'y';
end
if strcmp(answer3,yes) == 1
b = -(nstep-1):1:(nstep-1);
a = xcorr(x','coeff');
bb = 'Normalized auto-correlation sequence';
cc = 'Sequence lag';
figure(2)
plot(b,a'),...
title(aa), ylabel(bb), xlabel(cc), grid;
disp(' ');
disp('Check the window containing the Auto-correlation Graph and . . .');
disp('Press a key to continue . . . ');
pause
end
% Select another computation, if desired
disp(' ');
answer1 = input('Do you want another computation? (y/n)[y] --> ','s');
disp(' ');
if isempty(answer1)
answer1 = 'y';
end
end
disp('End of the program XRWALK');
disp(' ');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -