Matlab - a seemingly simple instruction causes high CPU utilization

I am writing a Matlab script that implements a noise reduction algorithm. It iterates over the length of the input signal (which I created in the same script) and processes the sample according to the sample. The algorithm does not require a lot of memory (it carries two values for the next iteration, namely, x_peak_oldand g_old). However, in order to control the internal process of the algorithm, I created several additional debug vectors that have the same input signal length and are updated with each cycle. I will comment on them if necessary.

However, my problem is that one of these vectors behaves strangely. If I turn off this vector and click "Run", the script will execute and end in an instant. However, when I assign values ​​to this particular vector every cycle and run the script, it takes several seconds to calculate, and one core of my processor is 100% loaded during this time.

Here is the code:

clear all; close all;

% ***** Parameter *****
Fs = 44100; % Samplerate
ta = 5; % Attack time in ms
tr = 300; % Release time in ms
th = -8; % Threshold in dB
R = 0.05; % Ratio in dB/dB

% ***** Test signal *****
f0 = 20000; % Frequency in Hz
a = (0:Fs) ./ Fs;
aflip = fliplr(a);
sine = sin((2 * pi * f0 / Fs) .* (0:Fs));
sinedecay = zeros(1, Fs);
for k = 1:(Fs+1)
    sinedecay(k) = sine(k)*aflip(k);
end
zeropad = zeros(1, Fs);
% Sine only
% x = [zeropad sine sinedecay zeropad];
% Triangle Rectangle Sine
x = [zeropad a (1-a) -a a-1 zeropad ones(1, Fs) (-1).*ones(1,Fs) zeropad sine sine sinedecay zeropad];
% Dirac
%x = [zeropad 1 zeropad];
siglen = length(x);
x_axis = (1:siglen) ./ Fs;

% ***** Output signal *****
y = zeros(1, siglen);

% ***** Debug signals *****
%threshold = 10^(th/20) .* ones(1, siglen);
%xpeakt = zeros(1, siglen);
%peaklogt = zeros(1, siglen);
%ctrl_logt = zeros(1, siglen);
f_t = zeros(1, siglen);
gt = zeros(1, siglen);

% ***** Derived parameters *****

AT = 1/(1+ta*Fs/1000);
RT = 1-1/(1+tr*Fs/1000);
TH_log = log2( 10^(th/20));
S = 1 - 1/R;

% ***** Initial parameters *****

x_peak_old = 0;
g_old = 0;

% ***** Lets go! *****

for i = 1:siglen 

% Peak measurement
    p0 = abs(x(i));
    x_peak = x_peak_old * RT;
    if (p0 > x_peak)
        x_peak = p0 * AT + x_peak_old *(1-AT);
    end
    x_peak_old = x_peak;
%    xpeakt(i) = x_peak; % DEBUG
    x_peak_log = log2(x_peak);
%    peaklogt(i) = x_peak_log; % DEBUG

% Compare with threshold

    ctrl_log = TH_log - x_peak_log;
    if (ctrl_log < 0)
        ctrl_log = 0;
    end
%    ctrl_logt(i) = ctrl_log; %DEBUG

% Calculation of weight factor

    f = S * ctrl_log;
    f_t(i) = f; %DEBUG
    g = 2^f;

% Smoothing filter for weight factor
    if (g > g_old) 
        coeff = 1-AT;
    else
        coeff = 1-RT;
    end
    temp = g;    
    g = coeff * g + (1-coeff) * g_old;
    g_old = temp;

    % THE FOLLOWING LINE CAUSES PROBLEMS
    gt(i) = g; % DEBUG

% Calculation output signal
    y(i) = x(i) * g;
end

% ***** Plot input, output and debug signals *****
figure;
subplot(211);
plot(x_axis, x);
hold on; grid on;
%plot(x_axis, threshold);
%plot(x_axis, xpeakt); %DEBUG
%plot(x_axis, peaklogt); %DEBUG
%plot(x_axis, ctrl_logt); %DEBUG
plot(x_axis, f_t); %DEBUG
%plot(x_axis, gt); %DEBUG
subplot(212);
plot(x_axis, y);

The line causing the problem is near the end:

gt(i) = g; % DEBUG

However, just a few lines before, in the same loop, I get the value for the vector in the same way:

f_t(i) = f; %DEBUG

Both vectors are initialized the same way. This cycle has more vectors that are processed in exactly the same way, but only the behavior of the vector gt(i)behaves oddly.

script , Matlab R2014b. ?

+4
2

gt ( ).


, . Mathworks , , .

gt >.

script:

>> which gt
built-in (C:\TLAB13a\toolbox\matlab\ops\@double\gt)  % double method

script:

>> which gt
gt is a variable.

Matlab R2013a (~ 7.5 , 0,1 ).

, R2016a (~ 0,12 ). Matlab .


, Matlab, gt - ( ), gtx, , script , .

+4

. -, gt, , ' > '. , . .

+3

Source: https://habr.com/ru/post/1671876/


All Articles