1
function [out, lower_prct, upper_prct] = RaznFilter(signal, threshold)
3
% signal = signal-medfilt1(signal, Nmed);
5
% % Ïåðâàÿ ðàçíîñòü ñèãíàëà
7
% FirstRaznSignal = [signal(1), signal];
8
% FirstRaznSignal = FirstRaznSignal-[signal, signal(len)];
10
% disp("Àïåðòóðà ôèëüòðà äëÿ insFrc_AcfNrm äëÿ ôèëüòðà ïðîöåíòèëåé: " + Nmed);
11
% prct = prctile(FirstRaznSignal, threshold);
16
% disp("lower_prct " + lower_prct);
17
% disp("upper_prct " + upper_prct);
19
% % signal=signal-medfilt1(signal, Nmed);
20
% out = FirstRaznSignal;
23
% plot(FirstRaznSignal,'blue--'); hold on; grid on;
24
% line('XData', [0 200], 'YData', [upper_prct upper_prct], 'Color','black','LineStyle','--');
25
% line('XData', [0 200], 'YData', [lower_prct lower_prct], 'Color','black','LineStyle','--');
28
% MemoryValue = FirstRaznSignal(1);
30
% % Öèêë ïî âñåìó ñèãíàëó
31
% for value = FirstRaznSignal
32
% if (value < lower_prct || value > upper_prct)
33
% out(index) = MemoryValue;
35
% MemoryValue=out(index);
38
% plot(out,'red'); grid on;
39
% ylabel("Hz",'interp','none'); xlabel("ns",'interp','none'); title("Ïåðâàÿ ðàçíîñòü"); grid on;
40
% legend('Ïåðâàÿ ðàçíîñòü','down','up','Èíòåðïîëÿöèÿ', 'Location', 'southoutside');
43
% Âû÷èñëåíèå ïåðâîé ðàçíèöû (àïïðîêñèìàöèÿ ïðîèçâîäíîé)
44
difference = diff(signal);
46
% Îïðåäåëåíèå ïîðîãà äëÿ ñãëàæèâàíèÿ ïðîâàëîâ
47
% threshold = 5; % Ïðèìåð ïîðîãîâîãî çíà÷åíèÿ (çàìåíèòå íà âàøå ðåàëüíîå çíà÷åíèå)
48
prct = prctile(difference, threshold);
53
% figure('Position', [0 0 900 800]);
54
% plot(difference,'blue'); hold on; grid on;
56
% line('XData', [0 len], 'YData', [upper_prct upper_prct], 'Color','black','LineStyle','--');
57
% line('XData', [0 len], 'YData', [lower_prct lower_prct], 'Color','black','LineStyle','--');
59
% line('XData', [0 200], 'YData', [threshold threshold], 'Color','green','LineStyle','--');
60
% line('XData', [0 200], 'YData', [-threshold -threshold], 'Color','green','LineStyle','--');
62
% % Ïðèìåíåíèå âçâåøåííîãî ñãëàæèâàíèÿ íà îñíîâå ïîðîãà
63
% smoothedSignal = signal;
64
% for i = 1:(length(difference))
70
% %if abs(difference(i)) > threshold
71
% if (difference(i) < lower_prct || difference(i) > upper_prct)
72
% % Åñëè ðàçíèöà ïðåâûøàåò ïîðîã, âûïîëíÿåì ñãëàæèâàíèå
73
% % smoothedSignal(i+1) = (signal(i) + signal(i+1)) / 2; % Ïðîñòîå ñãëàæèâàíèå ïî ñðåäíåìó çíà÷åíèþ
75
% while (difference(j) < lower_prct || difference(j) > upper_prct) && j ~= length(difference)-1
78
% smoothedSignal(i+1) = (signal(i) + signal(i+1)) / 2;
82
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83
% Âû÷èñëåíèå ïåðâîé ðàçíèöû (àïïðîêñèìàöèÿ ïðîèçâîäíîé)
84
% difference = diff(signal);
86
% Îïðåäåëåíèå ïîðîãà äëÿ ñãëàæèâàíèÿ ïðîâàëîâ
87
threshold = 0.1; % Ïðèìåð ïîðîãîâîãî çíà÷åíèÿ (çàìåíèòå íà âàøå ðåàëüíîå çíà÷åíèå)
89
% Ïðèìåíåíèå âçâåøåííîãî ñãëàæèâàíèÿ íà îñíîâå ïîðîãà
90
smoothedSignal = signal;
91
for i = 1:length(difference)
92
if abs(difference(i)) > threshold
93
% Åñëè ðàçíèöà ïðåâûøàåò ïîðîã, âûïîëíÿåì ñãëàæèâàíèå
94
smoothedSignal(i+1) = (signal(i) + signal(i+1)) / 2; % Ïðîñòîå ñãëàæèâàíèå ïî ñðåäíåìó çíà÷åíèþ
95
% Ïðîâåðÿåì ñëåäóþùèå îòìåòêè íà ïðåâûøåíèå ïîðîãà è ñãëàæèâàåì èõ
97
while j < length(difference) && abs(difference(j)) > threshold
98
smoothedSignal(j+1) = (signal(j) + signal(j+1)) / 2; % Ñãëàæèâàåì
103
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105
out = smoothedSignal;
108
% plot(out,'red'); grid on;
109
% plot(signal,'green--'); grid on;
110
% ylabel("Hz",'interp','none'); xlabel("ns",'interp','none'); title("Ïåðâàÿ ðàçíîñòü"); grid on;
111
% legend('Ïåðâàÿ ðàçíîñòü','down','up','Èíòåðïîëÿöèÿ', 'Location', 'southoutside');