I have a function that finds and adjusts peaks based on the rest of the signal. The problem I have is if the signal has 8000 points it works great and is very fast and finishes in about 1min but if it has 200000 points it takes an hour+. Any ideas how to speed up this function?
%example [peaks_found,peak_sig_adj]= rtadjpeak (signal, 3,3)
function [peaks_found,peak_sig_adj] =rtadjpeak (signal, T_multi_lower, T_multi_upper)
Af=signal;
% Thresholds
T_lower = -mean(abs(signal))*T_multi_lower
T_upper =mean(abs(signal))*T_multi_upper
% initialisation
[peaks_r,peaks_c] = find( Af < T_lower | Af > T_upper);
peaks = find( Af < T_lower | Af > T_upper);
%find the mean of all the peaks
counter = 0;
while ~isempty(peaks)
peaks = find( Af < T_lower | Af > T_upper);
try
Af(peaks) = ( Af(peaks-1) + Af(peaks+1) ) / 2;
catch
if peaks(1) == 1
Af(1) = 0;
else
Af(end) = 0;
end
end
counter=counter+1;
end
peaks_found=counter
peak_sig_adj=Af;
end
PS: I'm using octave 3.8.1
I did the profiler like someone recommended but I'm still at a lost as how to improve the speed of this function
profile on;
rtadjpeak(z_sig_combined_L1, 3, 3);
profile off;
>>>T_lower = -0.50551
>>>T_upper = 0.50551
>>>peaks_found = 1013
profshow (profile ("info"));
# Function Attr Time (s) Calls
---------------------------------------------
14 find 0.043 1017
5 binary < 0.023 1017
1 rtadjpeak 0.023 1
6 binary > 0.022 1019
20 binary | 0.018 1015
23 binary + 0.002 3036
22 binary - 0.002 1013
17 binary / 0.001 1013
21 isempty 0.000 1014
8 prefix ! 0.000 1016
2 abs 0.000 2
3 mean 0.000 2
16 sum 0.000 2
25 profile 0.000 1
9 false 0.000 3
4 nargin 0.000 7
13 size 0.000 2
7 isnumeric 0.000 2
10 binary == 0.000 4
11 true 0.000 2