-
Notifications
You must be signed in to change notification settings - Fork 0
/
geiger.m
134 lines (134 loc) · 5.04 KB
/
geiger.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
% Geiger Counter in MATLAB
% Version 1.0
% Alexey V. Voronin @ FoxyLab © 2015
% https://acdc.foxylab.com
% -----------------------------------
% ñåðèÿ èç íåñêîëüêèõ èçìåðåíèé
clc; % î÷èñòêà îêíà êîìàíä
close all; % óäàëåíèå ôèãóð
disp('***** Geiger counter *****');
disp('***Moving average algorithm***');
disp('Alexey V. Voronin @ FoxyLab © 2015');
disp('https://acdc.foxylab.com');
disp('**************************');
%ââîä äëèòåëüíîñòè èçìåðåíèé, ñåêóíäû
prompt = {'Measurement period, sec'};
defans = {'900'};
answer = inputdlg(prompt,'Measurement period',1,defans);
[nums status] = str2num(answer{1});
if ~status
error('Incorrect value!');
end;
disp(sprintf('T = %d sec',nums));
%ââîä øèðèíû îêíà "ñêîëüçÿùåãî ñðåäíåãî", ñåêóíäû
prompt = {'Moving average window size, sec'};
defans = {'900'};
answer = inputdlg(prompt,'Moving average window size',1,defans);
[window status] = str2num(answer{1});
if ~status
error('Incorrect value!');
end;
disp(sprintf('W = %d sec',window));
Fs = 44100; % ÷àñòîòà îöèôðîâêè
duration = 1; % äëèòåëüíîñòü èçìåðåíèÿ = 1 ñåê
threshold = 0.5; % ãðàíèöà èìïóëüñà
pause = 5; % äëèíà ïàóçû â ñýìïëàõ
ai = analoginput('winsound'); % creates the analog input object AI for a sound card having an ID of 0 (adaptor must be winsound)
addchannel(ai,1);
set (ai, 'SampleRate', Fs); %óñòàíîâêà ÷àñòîòû îöèôðîâêè
set (ai, 'SamplesPerTrigger', duration*Fs); %çàäàíèå ÷èñëà ñýìïëîâ äëÿ çàõâàòà
set (ai, 'TriggerType', 'Manual'); %ðó÷íîé ñòàðò çàõâàòà
set(ai,'TriggerRepeat',Inf);
start(ai); %ãîòîâíîñòü ê íà÷àëó çàõâàòà
MAs = []; %ñîçäàåì ìàññèâ äëÿ õðàíåíèÿ âñåõ MA
MABuffer = []; %ñîçäàåì ìàññèâ äëÿ ïîäñ÷åòà MA
MASize = window; %ðàçìåð áóôåðà ðàâåí ðàçìåðó îêíà
MACount = 0; %ñáðîñ ñ÷åò÷èêà çàïîëíåíèÿ áóôåðà
for m =1:1:MASize %î÷èñòêà áóôåðà
MABuffer(m) = 0;
end;
pulse = false; %ñáðîñ ôëàãà èìïóëüñà
pauseCount = 0; % îáíóëåíèå äëèòåëüíîñòè ïàóçû
count = 0; %îáíóëåíèå ñ÷åò÷èêà MA
trigger(ai); %íà÷àëî çàõâàòà
while (count < nums) %öèêë èçìåðåíèé
data = getdata(ai); %÷òåíèå äàííûõ
trigger(ai); % the data samples will not be stored in the data acquisition engine until the TRIGGER command is issued
% GETDATA is a "blocking" function. This means that it will wait until all data have been collected
size = length(data); %ðàçìåð ìàññèâà äàííûõ
pulseCount = 0; %îáíóëåíèå ñ÷åò÷èêà èìïóëüñîâ â èçìåðåíèè
for i = 1:1:size %öèêë ïî âñåì ñýìïëàì
%åñëè èìïóëüñ åùå íå íà÷àëñÿ
if (pulse == false)
if (abs(data(i))>threshold)
%åñëè óðîâåíü ïðåâûøàåò ïðåäåë, òî ïîäíèìàåì ôëàã íà÷àëà èìïóëüñà
pulse = true;
end
else
% åñëè èìïóëüñ óæå íà÷àëñÿ
if (abs(data(i))<=threshold)
% åñëè óðîâåíü íèæå ïðåäåëà
pauseCount = pauseCount + 1;% èíêðåìåíò ñ÷åò÷èêà äëèòåëüíîñòè ïàóçû
if (pauseCount > pause)
% åñëè ïàóçà â òå÷åíèå íóæíîãî âðåìåíè
pulse = false;% ôëàã èìïóëüñà ñïóùåí, áóäåì æäàòü íîâîãî èìïóëüñà
pulseCount = pulseCount + 1;% èíêðåìåíò ñ÷åò÷èêà èìïóëüñîâ
pauseCount = 0;% îáíóëåíèå äëèòåëüíîñòè ïàóçû
end
else
% ïàóçà ïðåðâàíà äîñðî÷íî
pauseCount = 0;
end
end
end;
%èíêðåìåíò ñ÷åò÷èêà çàïîëíåíèÿ áóôåðà
if (MACount < MASize)
MACount = MACount + 1;
end;
%ñìåùåíèå äàííûõ â áóôåðå ââåðõ
m = MASize;
while m>1
MABuffer(m) = MABuffer(m-1);
m = m-1;
end;
%ñîõðàíåíèå òåêóùåãî ðåçóëüòàòà èçìåðåíèé â áóôåðå
MABuffer(1) = pulseCount;
%ïîäñ÷åò MA
MA = 0;
for m =1:1:MACount
MA = MA+MABuffer(m);
end;
MA = MA / MACount * 60 / duration; %óñðåäíÿåì MA çà 1 ìèíóòó
disp(strcat(sprintf('Full = %0.1f',MACount/MASize*100),'%'));% âûâîä % çàïîëíåíèÿ áóôåðà
disp(sprintf('MA = %0.2f CPM',MA));% âûâîä MA
count = count + 1;%èíêðåìåíò ñ÷åò÷èêà MA
MAs(count) = MA;%ñîõðàíåíèå MA
end;
% ñòðîèì ãðàôèê MA
x_s = [0:duration:(count-1)*duration];%ìåòêè ïî îñè x
h = figure(1);
plot(x_s,MAs,'LineWidth',3);%ãðàôèê MA
grid on;
ylim([0 2*duration*60]); % ïðåäåëû ïî îñè OY
title('Moving Average');
xlabel('t, sec') % ìåòêà îñè OX
ylabel('MA, CPM') % ìåòêà îñè OY
%ñîõðàíåíèå MA â ôàéëå
formatOut = 'yyyymmddHHMMSS';
unique = datestr(now,formatOut);
% ñîñòàâëÿåì èìÿ ôàéëà
unique_png = strcat(unique,'.png');
%Ñîõðàíåíèå ãðàôèêà â ôàéë c íóæíûì ðàñøèðåíèåì (ñìîòðè help)
saveas(h, unique_png, 'png');
close(h); %çàêðûâàåì ïîñòðîåííûé ãðàôèê, ÷òîá íå çàñîðÿòü ïàìÿòü
unique_txt = strcat(unique,'.txt');
dlmwrite(unique_txt,MAs,'precision',6)
MA_min = min(MAs); % ìèíèìàëüíîå ÷èñëî èìïóëüñîâ
MA_max = max(MAs); % ìàêñèìàëüíîå ÷èñëî èìïóëüñîâ
disp(sprintf('Min = %0.2f CPM',MA_min));
disp(sprintf('Max = %0.2f CPM',MA_max));
stop(ai); % stop the analog input object from running
delete(ai); % delete the analog input object to free memory and other physical resources
% clears the finished object from the MATLAB workspace
clear ai;
clear all; % deletes all objects from memory