-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsii.R
364 lines (305 loc) · 10.3 KB
/
sii.R
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
sii <- function(
speech=c("normal","raised","loud","shout"),
noise,
threshold,
loss,
freq,
method=c(
"critical",
"equal-contributing",
"one-third octave",
"octave"
),
importance=c(
"SII",
"NNS",
"CID22",
"NU6",
"DRT",
"ShortPassage",
"SPIN",
"CST"
),
interpolate=FALSE
)
{
## Assumptions:
##
## freq: If provided, frequencies in Hz at which speech, noise, and/or
## threshold values are measured. If missing, frequencies will
## corresponding to those utilized by the specified method.
## Note that, frequencies must be provided if "interpolate=TRUE"
##
## speech: Speech level in dB at each frequency, or one of levels of
## stated vocal effort ("raised", "normal", "loud", "shout")
## provided by the standard, in which case the reference levels
## in dB will be applied.
##
## noise: Noise dB at each frequency, defaults to -50 dB at each
## frequency (as required by ANSI S3.5-1997 section 4.2)
##
## threshold: Hearing threshold level in dB at each frequency. If
## missing, assumed to be 0.
##
## loss: Hearing threshold loss factor due to the presence of
## conductive hearing loss in dB. If missing, assumed to be 0
## Determine which method will be used
method=match.arg(method)
## Get the appropriate table of constants
data.name <- switch(method,
"critical"="critical",
"one-third octave"="onethird",
"equal-contributing"="equal",
"octave"="octave"
)
data(list=data.name, package="SII")
table <- get(data.name)
## Get the correct importance functions
if(missing(importance) || is.character(importance) )
{
importance=match.arg(importance)
if(importance!="SII")
{
sic.name <- paste("sic.",data.name, sep="")
data(list=sic.name, package="SII")
sic.table <- get(sic.name)
table[,"Ii"] <- sic.table[[importance]]
}
}
else
if(length(importance) != nrow(table))
stop("`importance' vector must have length ", nrow(table), " for method `",method,"'.")
else
table[,"Ii"] <- importance
## Handle missing freq
if(missing(freq))
if(interpolate)
stop("`freq' must be specified when `interpolate=TRUE'")
else
freq <- table$fi
## Get appropriate reference values for speech
if(is.character(speech))
{
const.speech=TRUE
speech <- match.arg(speech)
speech <- table[[speech]]
}
else
const.speech=FALSE
## Handle missing noise
if(missing(noise))
noise <- rep(-50, length(freq))
## Handle missing threshold
if(missing(threshold))
threshold <- rep(0, length(freq))
## Handle missing loss
if(missing(loss))
loss <- rep(0, length(freq))
## Ensure that speech, noise, and threshold are the correct size
nfreq <- length(freq)
if(length(speech) != nfreq && !const.speech)
stop("`speech' must have the same length as `freq'.")
if(
length(noise) != nfreq ||
length(threshold) != nfreq ||
length(loss) != nfreq
)
stop("`noise', `threshold', and `loss` must have the same length as `freq'.")
## Check for missing values
any.nas <- any(is.na(c(noise,threshold,loss,freq)) )
if(!const.speech)
any.nas <- any.nas || any(is.na(speech))
if(any.nas && !interpolate)
stop("Missing values only permitted when `interpolate=TRUE'")
## Sort values into frequency order
ord <- order(freq)
freq <- freq [ord]
noise <- noise [ord]
threshold <- threshold[ord]
loss <- loss [ord]
if(!const.speech)
speech <- speech [ord]
## Store these values in the return object.
retval <- list()
retval$call <- match.call()
retval$orig <- list( freq, speech, noise, threshold, loss )
if(interpolate)
{
sii.freqs <- table[,"fi"]
approx.l <- function(obs.freq,value,target.freq)
{
nas <- is.na(value)
if(any(nas))
{
warning(sum(nas), " missing values ommitted")
value <- value[!nas]
obs.freq <- obs.freq[!nas]
}
tmp <- approx(
x=log10(obs.freq),
y=value,
xout=log10(target.freq),
method="linear",
rule=2
)
tmp$y
}
#debug(approx.l)
## Interpolate unobserved frequencies
noise <- approx.l(freq, noise, sii.freqs)
threshold <- approx.l(freq, threshold, sii.freqs)
loss <- approx.l(freq, loss, sii.freqs)
if(!const.speech)
speech <- approx.l(freq, speech, sii.freqs)
freq <- sii.freqs
}
#########
## Calcuate SII following ANSI S3.5-1997 Section 4
#########
## Setup: Create worksheet
col.names <- c("Fi", "E'i", "N'i", "T'i", "Vi", "Bi", "Ci", "Zi",
"Xi", "X'i", "Di", "Ui", "Ji", "Li", "Ki", "Ai",
"Ii", "IiAi")
sii.tab <- matrix(nrow=length(freq),ncol=length(col.names))
colnames(sii.tab) <- col.names
rownames(sii.tab) <- 1:nrow(sii.tab)
sii.tab <- as.data.frame(sii.tab)
#####
## Step 1: Select calculation method
#####
## The calculation method is already stored in 'method'
## Copy midband frequencies into the table
sii.tab$"Fi" <- freq
#####
## Step 2: Equivalent speech E'i, noise N'i, and hearing threshold
## T'i spectra
#####
sii.tab$"E'i" <- speech
sii.tab$"N'i" <- noise
sii.tab$"T'i" <- threshold
sii.tab$"Ji" <- loss
#####
## Step 3: Equivalent masking spectrum level (Zi)
#####
if(method=="octave")
## 4.3.1
sii.tab$"Zi" <- sii.tab$"N'i"
else
{
## 4.3.2.1 self-speech masking level
sii.tab$"Vi" <- sii.tab$"E'i" - 24
## 4.3.2.2
sii.tab$"Bi" <- pmax(sii.tab$"N'i", sii.tab$"Vi")
## 4.3.2.3 slope per octive of spread of masking, Ci
if(method=="critical" ||
method=="equal-contributing")
{
sii.tab$"Ci" <- -80 + 0.6*( sii.tab$"Bi" + 10*log10(table$"hi" - table$"li") )
}
else # method=="one-third octave"
{
sii.tab$"Ci" <- -80 + 0.6*( sii.tab$"Bi" + 10*log10(table$"fi") - 6.353 )
}
if(method=="critical" ||
method=="equal-contributing")
{
Zifun <- function(i)
{
slow <- TRUE
if(slow)
{
accum <- 10 ^ (0.1 * sii.tab[i,"N'i"])
if(i>1)
for(k in 1:(i-1))
accum <- accum + 10 ^ (0.1 * (sii.tab[k,"Bi"] + 3.32*sii.tab[k,"Ci"] * log10( table[i,"fi"] / table[k,"hi"] ) ) )
retval <- 10 * log10(accum)
}
else
{
if(i>1)
inner <- sum( 10 ^ (0.1 * ( sii.tab[1:(i-1),"Bi"] + 3.32*sii.tab[1:(i-1),"Ci"] * log10( table[i,"fi"] / table[1:(i-1),"hi"] ) ) ) )
else
inner <- 0
retval <- 10 * log10( 10 ^ (0.1 * sii.tab[i,"N'i"] ) + inner )
}
retval
}
sii.tab$"Zi" = sapply(1:nrow(sii.tab), Zifun)
}
else # method=="one-third octave"
{
Zifun <- function(i)
{
slow <- FALSE
if(slow)
{
accum <- 10 ^ (0.1 * sii.tab[i,"N'i"])
if(i>1)
for(k in 1:(i-1))
accum <- accum + 10 ^ (0.1 * ( sii.tab[k,"Bi"] + 3.32*sii.tab[k,"Ci"] * log10( 0.89 * table[i,"fi"] / table[k,"fi"] ) ) )
retval <- 10 * log10(accum)
}
else
{
if(i>1)
inner <- sum( 10 ^ (0.1 * ( sii.tab[1:(i-1),"Bi"] + 3.32*sii.tab[1:(i-1),"Ci"] * log10( 0.89 * table[i,"fi"] / table[1:(i-1),"fi"] ) ) ) )
else
inner <- 0
retval <- 10 * log10( 10 ^ (0.1 * sii.tab[i,"N'i"] ) + inner )
}
}
sii.tab$"Zi" = sapply(1:nrow(sii.tab), Zifun)
}
## 4.3.2.4
sii.tab[1,"Zi"] <- sii.tab[1,"Bi"]
}
#####
## Step 4: Equivalent internal noise spectrum level, X'i
#####
## Copy reference internal noise spectrum Xi
sii.tab$"Xi" <- table$"Xi"
## Calculate X'i
sii.tab$"X'i" <- sii.tab$"Xi" + sii.tab$"T'i"
#####
## Step 5: Equivalent disturbance spectrum, Di
#####
sii.tab$"Di" <- pmax( sii.tab$"Zi", sii.tab$"X'i" )
#####
## Step 6: Level distortion factor, Li
#####
## Standard speech spectrum level at normal vocal level Ui
sii.tab$"Ui" <- table$"normal"
## Calculate speech level distortion factor Li
enforce.range <- function(x)
{
x[x < 0] <- 0 # min is 0
x[x > 1] <- 1 # max is 1
x
}
## Formula A1, which extends formula 11 to handle conductive
## hearing loss (Ji)
sii.tab$"Li" <- 1 - (sii.tab$"E'i" - sii.tab$"Ui" - 10 - sii.tab$"Ji" )/160
sii.tab$"Li" <- enforce.range(sii.tab$"Li")
## Step 7: Calculate Ki
sii.tab$"Ki" <- (sii.tab$"E'i" - sii.tab$"Di" + 15)/30
sii.tab$"Ki" <- enforce.range( sii.tab$"Ki" )
## Calculate Ai
sii.tab$"Ai" <- sii.tab$"Li" * sii.tab$"Ki"
## Step 8: Copy band importance function values
sii.tab[,"Ii"] <- table[,"Ii"]
## Calculate Ii * Ai
sii.tab[,"IiAi"] <- table[,"Ii"] * sii.tab[,"Ai"]
## Sum IiAi to determine SII
sii.val <- sum(sii.tab[,"IiAi"])
## Package it all up to return to the user
retval$speech <- speech
retval$noise <- noise
retval$threshold <- threshold
retval$loss <- loss
retval$freq <- freq
retval$method <- method
retval$table <- sii.tab
retval$sii <- sii.val
class(retval) <- "SII"
retval
}