-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path2_HMDwithLFAS_RRPCA.R
213 lines (175 loc) · 6.33 KB
/
2_HMDwithLFAS_RRPCA.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
# De-noising methods for ambient samples
# reads in all 1-min HMD data with AS labels
# selects only Ambient samples, and runs RRPCA
# saves out updated persite files
rm(list=ls())
library(data.table)
library(ggplot2)
library(lubridate)
library(dplyr)
library(viridis)
library(tidyverse)
library(rsvd)
#GET DATA ####
inDir = ( "F:\\SanctSound\\analysis\\combineFiles_AcousticScene" )
inFiles = list.files( inDir, pattern = "HMDdetLF", full.names = T)
inFiles = list.files( inDir, pattern = "HMDdetLF", full.names = T, recursive = T)
pltf = 0
fqr = "LF" #append this to output names
site = "All"
DC = Sys.Date()
dirOut = "F:\\SanctSound\\analysis\\combineFiles_AcousticScene"
#PREP DATA ####
Ambient = NULL
for (f in 1: length(inFiles)-1 ) { # f = 6 for testing
load( inFiles[f])
st = sapply(strsplit(basename( inFiles[f]), "_"), "[[", 2) #site name
HMDdet$Site = st
Ambient = rbind (Ambient, HMDdet[HMDdet$Category == "Ambient",] )
}
#CHECKS
unique(Ambient$Site)
length( which(is.na(Ambient)))
save(Ambient, file = paste0(dirOut, "\\Ambient_HMD_allSites_", DC, ".Rda") )
idNA = ( which(is.na(Ambient))) # check for NAs, as.data.frame( colnames( Ambient )[1:10] )
idx = grep("^X", colnames(Ambient))
hix = as.numeric( gsub("X","", names(Ambient)[idx]) )
Nv = Ambient[, idx] #dB values
NvP = 10^(Nv/20) #pressure values
nvDate = Ambient$dateTime
# truncate to 100-1kHz Acoustic Scene??
fe = which(hix == 1001.2)
NvPt = NvP[,1:fe]
Nv = Nv[ ,1:fe]
hix = hix[1:fe]
#ANALYSIS ####
# Robust principal components analysis separates a matrix into a low-rank plus sparse component
#a method for the robust separation of a rectangular (m, n) matrix A into a low-rank component L and a sparse component S
# input = ( NvP )
lamd = max(NvPt)^-0.5 #default settings
nvpcaTOL = rrpca(NvPt)
sampleHours = nrow(NvP)
save(nvpcaTOL, file = paste0(dirOut, "\\RRPCA_HMD_allSites_", DC, ".Rda") )
# START HERE TO LOAD FILES
load( paste0(dirOut, "\\Ambient_HMD_allSites_2023-08-15.Rda") )
idx = grep("^X", colnames(Ambient))
hix = as.numeric( gsub("X","", names(Ambient)[idx]) )
Nv = Ambient[, idx] #dB values
NvP = 10^(Nv/20) #pressure values
NvP = NvP[,1:698]
Nv = Nv[,1:698]
hix = hix[1:698]
load( paste0(dirOut, "\\RRPCA_HMD_allSites_2023-08-14.Rda" ))
#SUMMARIZE OUTPUT ####
#low rank
Lr = as.data.frame(nvpcaTOL$L)
colnames(Lr) = hix
LrDB = 10*log10( Lr^2 ) #CHECK: min(LrDB$`63`), no negative values, just values without transients
colnames(LrDB) = hix
#sparse matrix
Sp = as.data.frame(nvpcaTOL$S)
colnames(Sp) = hix
SpDB = 10*log10( (Sp)^2 ) # negative and zero values-- does not make sense to convert back to dB
colnames(SpDB) = hix
# Values for each minute ####
# sum of difference across frequencies for each minute
LRdiff = as.data.frame ( rowSums( abs ( (LrDB - Nv) ) ) )
colnames(LRdiff) = 'LRdiff'
LRfq = as.data.frame ( as.numeric ( colnames(LrDB) [apply(LrDB, 1, (which.max) )] ) )
colnames(LRfq) = 'LRfq'
# sum of sparce across frequencies for each minute
SPsum = as.data.frame ( rowSums( abs ( Sp ) ) )
colnames(SPsum) = 'SPsum'
#RECOMBINE WITH Site Files ACOUSTIC SCENE FILES ####
Ambient$LowRanK = as.numeric( as.character(LRdiff$LRdiff ) )
Ambient$Sparce = as.numeric( as.character(SPsum$SPsum ) )
Ambient$LRfq = LRfq$LRfq
RRPCAsumOUT = NULL
for (f in 1: length(inFiles) ) { # f = 6 for testing
# all HMD data for a site
load( inFiles[f])
st = sapply(strsplit(basename( inFiles[f]), "_"), "[[", 2) #site name
HMDdet$Site = st
# WHY ARE THEIR DUPLICATED ROWS!!! with different values
# which( duplicated( HMDdet$dateTime))
# only Ambient data for a specific site
tmp = Ambient[ Ambient$Site == st, c(1, 1006:1008)]
# nrow( HMDdet[HMDdet$Category == "Ambient", ])
# merge data to include rrpca analysis results to HMD data
tst = merge(HMDdet, tmp, by = "dateTime", all.x = T)
#hist( tst$LRfq )
#hist( tst$LowRanK )
#hist( tst$Sparce )
RRPCAsum = as.data.frame ( rbind ( quantile(tst$LowRanK, na.rm = T),
quantile(tst$LRfq, na.rm = T),
quantile(tst$Sparce, na.rm = T) ) )
RRPCAsum$Site = st
RRPCAsum$RRPCAmetric = c("LR-sum","LR-freq","SP-sum")
RRPCAsumOUT = rbind(RRPCAsumOUT, RRPCAsum )
# write out new HMD+ file per site
save(tst, file = paste0(dirOut, "\\HMDdetsRpca_", st, "_", DC, ".Rda") )
}
RRPCAsumOUT
## NOT USED ####
# TRUNCATE TO FIGURE OUT WHAT IS GOING ON ####
tLrDB = LrDB[1:100,]
tSpDB = Sp[1:100,]
tAm = Nv[1:100,]
tnvDate = Ambient$dateTime[1:100]
#original matrix
NvMO = reshape :: melt(t(tAm) )
head(NvMO)
NvMO$X1 = as.numeric( gsub("X","", NvMO$X1) )
pO = ggplot(NvMO, aes(X1, value, group = as.factor(X2)))+
geom_line(alpha=.05)+
scale_x_continuous(trans = "log10")+
xlab("Frequency (HMD)") + ylab("1-min HMD")+
theme_minimal()
pO
#LR matrix
LRMO = reshape :: melt(t(tLrDB) )
head(LRMO)
LRMO$X1 = as.numeric( gsub("X","", LRMO$X1) )
pL = ggplot(LRMO, aes(X1, value, group = as.factor(X2)))+
geom_line(alpha=.05)+
scale_x_continuous(trans = "log10")+
xlab("Frequency (HMD)") + ylab("1-min HMD")+
theme_minimal()
pL
#SP matrix
SPMO = reshape :: melt(t(tSpDB) )
head(SPMO)
SPMO$X1 = as.numeric( gsub("X","", SPMO$X1) )
pS = ggplot(SPMO, aes(X1, value, group = as.factor(X2)))+
geom_line(alpha=.05)+
scale_x_continuous(trans = "log10")+
xlab("Frequency (HMD)") + ylab("1-min HMD")+
theme_minimal()
pS
#RECOMBINE WITH DAILY Site ACOUSTIC SCENE FILES ####
# not complete
inFilesDay = list.files( inDir, pattern = "DAILY_MILLIDEC_MinRes_LFAS.csv", full.names = T)
for (f in 1: length(inFilesDay)) { # f = 6 for testing
zmp = read.csv ( inFilesDay[f])
st = sapply(strsplit(basename( inFilesDay[f]), "_"), "[[", 1) #site name
}
# IS there sparce detection present-- how to summmarize?
library(dplyr)
idx = 2
iN = subset(NvMO, X2 == idx)
iL = subset(LRMO, X2 == idx)
iS = subset(SPMO, X2 == idx)
ggplot() +
geom_line(data=iN, aes(x=X1, y = value), color= "red") +
geom_line(data=iL, aes(x=X1, y=value), color = "blue") +
geom_line(data=iS, aes(x=X1, value), color = "green") +
theme_minimal()
# what is the difference between ambient and low rank?
NvL = abs ( diff(iN$value - iL$value) )
hist( NvL )
quantile(abs( SPMO$value) )
sum(NvL)
# sum of difference across frequencies for each minute
LRdiff = as.data.frame ( rowSums( abs ( (tLrDB - tAm) ) ) )
# sum of sparce across frequencies for each minute
SPsum = hist ( rowSums( abs ( tSpDB ) ) )