필터 지우기
필터 지우기

Error in my code

조회 수: 1 (최근 30일)
Mertcan
Mertcan 2024년 1월 6일
댓글: Mertcan 2024년 1월 6일

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
Dyuman Joshi
Dyuman Joshi 2024년 1월 6일
편집: Dyuman Joshi 2024년 1월 6일
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 2024년 1월 6일
I want to achieve index number of peak power frequencies.

댓글을 달려면 로그인하십시오.

답변 (1개)

Hassaan
Hassaan 2024년 1월 6일
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

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by