# Adaptiv whitening as defined by:

#@INPROCEEDINGS{Stowell2007Onset,
#    author = {Stowell, Dan and Plumbley, Mark},
#    title = {Adaptive whitening for improved real-time audio onset detection},
#    booktitle = {Proceedings of the International Computer Music Conference (ICMC'07)},
#    year = {2007},
#    pages = {312--319}
#}

# INPUT: 	fa: output of fftArray() function
#			ctrl: control object

# OUTPUT: 	fa like in input but with modificated spectrum

adaptivWhitening = function(fa, ctrl)
{
  if (ctrl$aw.r == 999 & ctrl$aw.m == 999) {
	return(fa)
   } else{
  	S = fa$spec
 	r = ctrl$aw.r
  	m = ctrl$aw.m
  
 	S1 <- pmax(abs(S), r)
 	for(i in 2:ncol(S)) S1[,i] <- pmax(S1[,i], m*S1[,i-1])
	
 	S = S / S1
 	fa$spec=S
  	return(fa)
   }
}