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

8 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 data and plot 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)

Sudarsanan A K
Sudarsanan A K on 4 Nov 2023
Hi Pasu,
I understand that you are looking for a MATLAB script that would enable you to find and plot the Root Mean Square (RMS) and Crest Factor from a collection MAT files.
You can achieve this as follows:
  • Load each MAT file one by one.
  • Calculate the RMS and Crest Factor and store the values in arrays.
  • Then, plot the RMS values and Crest Factors in separate subplots within a single figure.
Here is an example code:
%% I am creating some MAT files that store random data (for the purpose of complete demonstration)
numFiles = 123; % Total number of data files
for i = 1:numFiles
% Generate random data
data = rand(1, 1000); % Change the size of the data as per your requirement
% Save data to mat file
fileName = sprintf('bearing1_1_%d.mat', i);
yourDataVariable = data; % Assign the data to the variable name
save(fileName, 'yourDataVariable');
end
%% Here is the structure of MATLAB script you are looking for
% Initialize variables
numFiles = 123; % Total number of data files
rmsValues = zeros(1, numFiles);
crestFactors = zeros(1, numFiles);
% Loop through each data file
for i = 1:numFiles
% Load the data file
fileName = sprintf('bearing1_1_%d.mat', i);
data = load(fileName);
% Calculate RMS
rmsValues(i) = rms(data.yourDataVariable); % Replace 'yourDataVariable' with the actual variable name in your mat file
% Calculate Crest Factor
crestFactors(i) = max(abs(data.yourDataVariable)) / rmsValues(i);
end
% Plot RMS and Crest Factor
figure;
subplot(2, 1, 1);
plot(rmsValues);
title('RMS');
xlabel('Data File');
ylabel('RMS Value');
subplot(2, 1, 2);
plot(crestFactors);
title('Crest Factor');
xlabel('Data File');
ylabel('Crest Factor');
You can additionally refer the MathWorks documentation for better understanding of the "sprintf()" function that I have used in the example:
I hope this helps you resolve the issue.

Community Treasure Hunt

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

Start Hunting!