MATLAB busy for all the time after the code is executed
Afficher commentaires plus anciens
I have one more query. In my code it seems there is an infinite loop going on and the MATLAB is busy for so long that I had to stop the debugging. Initially I was facing the "array exceeding maximum array size preference" error which was in this line of the code below Step 2 : h_k = freq_signal - mean_env;. In that case, the error was likely caused by the fact that freq_signal or mean_env is a very large array. The subtraction operation between the two arrays would result in an array of the same size, which could potentially exceed MATLAB's maximum array size preference. To fix this issue, I tried breaking up the input signal into smaller segments and processing each segment separately. This would reduce the memory requirements of the operation and prevent MATLAB from running out of memory. So I did the following. Input signal freq_signal is broken up into segments of size segment_size, and the envelope() function is applied to each segment separately. The resulting mean_env arrays are then averaged to obtain the final mean_env
7 commentaires
Jan
le 13 Mar 2023
h_k = freq_signal - mean_env
If freq_signal and mean_env have the same size, this line cannot cause an "array exceeding maximum array size preference" error. If h_k exceeds the allowed size, the variables on the right side would also, so their creation would cause the error already.
The only possible cause of the error, is the automatic expansion, if one of the variables is a row and the other a column vector.
What is your question?
We cannot run your code due to the missing inputs.
Cris LaPierre
le 13 Mar 2023
Sharing the code here so it's easier to see
% Load the frequency signal from Simulink
load('IEEE_14_Bus_System_SM_FFR.mat', 'Fs');
freq_signal = Fs.signals.values;
% Set up parameters
sampling_rate = 1000; % Hz
window_size = round(sampling_rate/20); % Window size for envelope function
segment_size = 100000; % Number of samples in each segment
% Step 1: Find extrema and obtain upper and lower envelopes
num_segments = ceil(length(freq_signal)/segment_size);
mean_env = zeros(size(freq_signal));
for i = 1:num_segments
start_idx = (i-1)*segment_size + 1;
end_idx = min(i*segment_size, length(freq_signal));
segment = freq_signal(start_idx:end_idx);
[upper_env, lower_env] = envelope(segment, window_size, 'analytic');
mean_env(start_idx:end_idx) = mean_env(start_idx:end_idx) + (upper_env + lower_env)/2;
end
mean_env = mean_env/num_segments;
% Step 2: Subtract mean envelope from frequency signal
h_k = freq_signal - mean_env;
% Step 3: Check if h_k is an IMF
is_imf = false;
while ~is_imf
% Find local maxima and minima of h_k
[maxima, max_locs] = findpeaks(h_k);
[minima, min_locs] = findpeaks(-h_k);
minima = -minima;
extrema = sort([max_locs; min_locs]);
% Check if number of extrema and zero crossings differ by at most one
num_zero_crossings = length(find(diff(sign(h_k)) ~= 0));
if abs(length(extrema) - num_zero_crossings) <= 1
% Compute upper and lower envelopes
upper_env = interp1(extrema, h_k(extrema), 1:length(h_k), 'pchip', 'extrap');
lower_env = interp1(extrema, h_k(extrema), 1:length(h_k), 'pchip', 'extrap');
% Check if mean value of local maxima and minima envelopes is zero
mean_env = (upper_env + lower_env)/2;
if max(abs(mean_env)) < 1e-10
is_imf = true;
c_j = h_k;
else
% If not an IMF, update h_k and try again
h_k = h_k - mean_env;
end
else
% If not an IMF, update h_k and try again
h_k = h_k - mean(h_k);
end
end
% Step 4: Compute residual signal
residual_signal = freq_signal - c_j;
% Step 5: Compute derivative of residual signal
dt = 1/sampling_rate;
derivative_signal = diff(residual_signal)/dt;
Taimoor Khan Mehmand
le 14 Mar 2023
"Requested 2000001x2000001 (29802.4GB) array exceeds maximum array size preference."
It looks like you unintentionally subtracted a row vector and a column vector. Perhaps this is what you intended:
h_k = freq_signal(:) - mean_env(:);
Taimoor Khan Mehmand
le 14 Mar 2023
Taimoor Khan Mehmand
le 14 Mar 2023
Jan
le 14 Mar 2023
The problem is not inside:
if max(abs(mean_env)) < 1e-10
is_imf = true;
c_j = h_k;
but that the algroithm to calcualte mean_env does not produce such small values.
You can use the debugger or some fprintf commands to track the values of upper_env, lower_env and mean_env.
Réponses (1)
Cris LaPierre
le 13 Mar 2023
It sounds like you're encountering an infinite loop in your while loop. Your conditional is is_imf, so it looks like you are never entering the following condition.
if max(abs(mean_env)) < 1e-10
is_imf = true;
c_j = h_k;
This suggests your constraint is either not appropriate for your data, or there is a mistake in calculating mean_env, as the values do not match the assumptions made when writing the code.
Catégories
En savoir plus sur Time-Frequency Analysis dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!