Filter löschen
Filter löschen

Error in my code

1 Ansicht (letzte 30 Tage)
Mertcan
Mertcan am 6 Jan. 2024
Kommentiert: Mertcan am 6 Jan. 2024

In my code below i get error at here:
% Calculate features
[~, I] = max(P_k);
%peak power frequency
peakPowerFreq(k) = fvec(I);
It says: Unable to perform assignment because the left and right sides have a different number of elements.
Error in untitled5 (line 24)
peakPowerFreq(k) = fvec(I);

Because of this error, i cant extract frequency features of bearing signals.
Can you help please.

url = 'https://www.mathworks.com/supportfiles/predmaint/condition-monitoring-and-prognostics-using-vibration-signals/pdmBearingConditionMonitoringData.mat';
websave('pdmBearingConditionMonitoringData.mat',url);
load pdmBearingConditionMonitoringData.mat
% Define the number of data points to be processed.
numSamples = length(data);
% Define sampling frequency.
fs = 20E3; % unit: Hz
peakPowerFreq = zeros(numSamples, 1);
for k = 1:numSamples
%Get most up-to-date data.
curData = data{k};

% Calculate spectrogram.
[~, fvec, ~, P_k] = spectrogram(curData, 500, 450, 512, fs);

% Calculate features
[~, I] = max(P_k);
%peak power frequency
peakPowerFreq(k) = fvec(I);
end
% Plot Peak Power Frequency
figure;
plot(expTime, peakPowerFreq);
xlabel('Time (min)');
ylabel('Peak Power Frequency (Hz)');
title('Peak Power Frequency vs. Time');
grid on;

  2 Kommentare
Dyuman Joshi
Dyuman Joshi am 6 Jan. 2024
Bearbeitet: Dyuman Joshi am 6 Jan. 2024
What is the objective/idea behind these 2 lines of code?
What do you want to achieve with these?
% Calculate features
[~, I] = max(P_k)
%peak power frequency
peakPowerFreq(k) = fvec(I)
These are the lines related to the error.
Mertcan
Mertcan am 6 Jan. 2024
I want to achieve index number of peak power frequencies.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Hassaan
Hassaan am 6 Jan. 2024
url = 'https://www.mathworks.com/supportfiles/predmaint/condition-monitoring-and-prognostics-using-vibration-signals/pdmBearingConditionMonitoringData.mat';
websave('pdmBearingConditionMonitoringData.mat',url);
load pdmBearingConditionMonitoringData.mat
% Define the number of data points to be processed.
numSamples = length(data);
% Define sampling frequency.
fs = 20E3; % unit: Hz
peakPowerFreq = zeros(numSamples, 1);
for k = 1:numSamples
% Get the most up-to-date data.
curData = data{k};
% Calculate spectrogram.
[~, fvec, ~, P_k] = spectrogram(curData, 500, 450, 512, fs);
% Find the frequency with the peak power for each segment
% Assuming you want the peak across all segments
[P_max, I] = max(max(P_k, [], 2));
% If max(P_k) returns a row for each segment, I will be a row vector.
% We need to find the index related to the global maximum.
% peak power frequency
peakPowerFreq(k) = fvec(I);
end
% Plot Peak Power Frequency
figure;
plot(expTime, peakPowerFreq);
xlabel('Time (min)');
ylabel('Peak Power Frequency (Hz)');
title('Peak Power Frequency vs. Time');
grid on;
Please note that the line [P_max, I] = max(max(P_k, [], 2)); finds the maximum value across all columns (which are the frequency bins in each segment) and then across all segments.
However, if you want to find the maximum frequency for each time segment separately, you would have to iterate through the columns of P_k and find the maximum for each. It depends on how you want to interpret the spectrogram data. The code I provided finds the global maximum across all segments, which may or may not be what you need depending on your specific application.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering

Community Treasure Hunt

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

Start Hunting!

Translated by