MATLAB busy for all the time after the code is executed

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

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.
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;
So basically the previous problem was in this line h_k = freq_signal - mean_env (Step2). In this equation my input signal is 'freq_signal' which is a signal in my Simulink model and I use 'To Workspace' block to store it to MATLAB workspace. I use the format 'Structure With Time' and it is a scalar value not a vector (basically it has one column) with the 'field values' showing this 2000001x1 double. Therefore, to my understanding previously when the Step 2 was executed i.e. when the mean_env was subtracted from freq_signal, MATLAB encountered the following error
(Requested 2000001x2000001 (29802.4GB) array exceeds maximum array size preference. Creation of arrays greater than this limit may take a long time and cause MATLAB to become unresponsive.)
So to avoid this error I tried breaking up the input signal into smaller segments and processing each segment separately. This would reduce the memory requiremnets of the operation and prevent MATLAB from running out of memory. And this worked. After fixing this error, I ran the code and then MATLAB was busy and it entered an infinite loop I guess.
So my question is how to solve this infinte loop issue? I have attached the approach with explanation for which I wrote a code and shared it here. Moreover, I have attached my MODEL file as well so that if you want to run the code you need to run simulink model to load the input signal 'Fs' to the base workspace.
Stephen23
Stephen23 le 14 Mar 2023
Modifié(e) : Stephen23 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(:);
@Stephen23 Yes, but now the issue is with this line maybe (@Cris LaPierre pointed out in the comments below).
if max(abs(mean_env)) < 1e-10
is_imf = true;
c_j = h_k;
@Stephen23 I attached the approach with explanation of each step in the attcahement section. My code is for the appraoch that is attached.
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.

Connectez-vous pour commenter.

Réponses (1)

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

Produits

Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by