-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopy_of_VAD.m
More file actions
154 lines (135 loc) · 3.71 KB
/
Copy_of_VAD.m
File metadata and controls
154 lines (135 loc) · 3.71 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
clear
global minVar;
global maxSpFl;
global silenceCount;
global minEn;
global enThreshPr;
global releaseCount
global lastOutput
global releaseTemp
%variabile per gestire l'index di file massimo
N = 5;
%variabile per gestire l'index di file minimo
minIndex = 1;
for fileIndex = minIndex : N
%control parameters
input_name = sprintf('inputaudio%i.data',fileIndex);
output_char = sprintf('outputVADCopy%i.txt',fileIndex);
output_DATA = sprintf('outputDATA%i.data',fileIndex);
%opening input file
fid = fopen(input_name, 'r');
signalInput = fread(fid, inf, 'int8');
%opening char output file
outCharFile = fopen(output_char, 'w');
%opening DATA output file
outDATAFile = fopen(output_DATA, 'w');
Fs = 8000;
length = length(signalInput);
L = int32((1/Fs)*length*1000);
fprintf("arrayLength:%i\n",length);
fprintf("the length of file is: %i ms \n",L);
releaseCount = 3;
releaseTemp = releaseCount;
minVar = 0;
maxSpFl = 1;
silenceCount = 1;
enThreshPr = 200;
lastOutput = 0;
output = 0;
outChar = "0";
for i = 1:160:size(signalInput)
fprintf("start of buffer\n")
bufSize = 159;
voidBuff = zeros(1,bufSize+1);
if(length-i < 159)
bufSize = length - i;
end
buff = signalInput(i:i+bufSize);
if(bufSize<159)
buff(bufSize:159)=0;
fprintf("lastOutput")
end
fprintf("campione numero %i\n",i);
if(i<160)
minEn = sum(abs(buff).^2);
end
outChar = vad(buff,Fs);
fwrite(outCharFile,outChar);
if(outChar == "1")
fwrite(outDATAFile,buff, 'int8')
else
if(outChar == "0")
fwrite(outDATAFile,voidBuff, 'int8')
end
end
fprintf("output funzione: %c \n",outChar);
end
fclose(fid);
fclose(outDATAFile);
fclose(outCharFile);
clear
end
function[outChar] = vad(buffer,Fs)
global silenceCount;
global releaseCount
global lastOutput
global releaseTemp
output = vadBuf(buffer,Fs);
if(output == 1)
outChar = "1";
else
if((output==0) && lastOutput == 1)
if(releaseTemp > 0)
outChar = "1";
releaseTemp = releaseTemp-1;
output = 1;
silenceCount = silenceCount+1;
fprintf("Attento che questo sarebbe rumore\n")
else
outChar = "0";
releaseTemp = releaseCount;
output = 0;
silenceCount = silenceCount+1;
end
else
outChar = "0";
end
end
lastOutput = output;
end
function [output] = vadBuf(curBuffer,Fs)
global minVar;
global silenceCount;
global minEn;
global enThreshPr;
enThresh = enThreshPr*log(minEn);
varThresh = 18;
response = 0;
fprintf('buf: [');
fprintf('%g ', curBuffer);
fprintf(']\n');
L = (1/Fs)*length(curBuffer)*1000;
fprintf("arrayLength:%i\n",length(curBuffer));
fprintf("the length of file is: %i ms \n",L);
energy = sum(abs(curBuffer).^2);
Y = fft(curBuffer);
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
variance = var(P1);
fprintf("variance: %i \n",variance);
fprintf("energy: %i \n",energy);
if(energy-minEn > enThresh)
response = response + 1;
end
if(variance-minVar > varThresh)
response = response + 1;
end
if(response > 1)
output = 1;
else
output = 0;
minVar = ((minVar*silenceCount)+variance)/(silenceCount+1);
minEn = ((minEn*silenceCount)+energy)/(silenceCount+1);
end
end