How to calculate RMS and Crest Factor of separate data and combine into one file and one plot

35 views (last 30 days)
Hello,
In case that I have the separate 123 mat files (bearing1_1_1.mat to bearing 1_1_123.mat) and I would like to calculate RMS and Crest Factor of each file and combine to one file.
In addition, how to plot the RMS into the graph like an attached picture (RMS of data 1 to data 123), how should I write the script?
Thanks for your kind supports

Answers (1)

Mathieu NOE
Mathieu NOE on 1 Mar 2022
hello
this is my suggestion - maybe you will have to do a few mods according how the data is stored in your mat files (not supplied)
fileDir = pwd; % current directory or specify the target directory
S = dir(fullfile(fileDir,'bearing*.mat')); % get list of data files in directory
S = natsortfiles(S); % sort file names into natural order (what matlab does not do well) , see FEX :
%(https://fr.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort)
for k = 1:length(S)
data = load(fullfile(fileDir, S(k).name));
time = data.time;
signal = data.signal;
signal_rms(k) = sqrt(mean(signal.^2));
CrestFactor(k) = max(abs(signal))/signal_rms(k);
end
figure
plot((1:k),signal_rms)
figure
plot((1:k),CrestFactor)
FYI I created some dummy data files to test the above code
Fs = 1000;
dt = 1/Fs;
for ci = 1:123
baseFileName = ['bearing_1_1_' num2str(ci) '.mat'];
time = 0:dt:1; % time vector
signal = ci*sin(2*pi*ci*time)+ 0.1*ci*rand(size(time)); % non constant amplitude and freq signal vector
save(baseFileName,'time','signal');
end

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!