

using PyCall
pygui(:qt)
using PyPlot
using DSP
using MAT
push!(LOAD_PATH, "LOCALPKG/")
using LinearAcoustics_LOCAL
using DataFrames
#close("all")

SNR,Fs = 15.,8e3
room = 2
Nt = 560
t = 1000*collect(0:1/Fs:(Nt-1)/Fs)
Nf = floor(Int64,Nt/2)+1
f =collect(0:Fs/(2*Nf):(Nf-1)Fs/(2*Nf))./1000 
Nw = 700
Nm = [8;12;16;20;24;28;32]
Nm = 12

results = [
Dict("reg"=>"l2","model"=>"pwdm",  "src_opt" => 0, 
     "ir" => zeros(Nt), "ir0" => zeros(Nt), "tf" => zeros(Nf), "tf0" => zeros(Nf)),
Dict("reg"=>"l1","model"=>"pwdm",  "src_opt" => 0, 
     "ir" => zeros(Nt), "ir0" => zeros(Nt), "tf" => zeros(Nf), "tf0" => zeros(Nf)),
Dict("reg"=>"l21","model"=>"pwdm", "src_opt" => 0, 
     "ir" => zeros(Nt), "ir0" => zeros(Nt), "tf" => zeros(Nf), "tf0" => zeros(Nf)),
Dict("reg"=>"l2","model"=>"tesm",  "src_opt" => 1, 
     "ir" => zeros(Nt), "ir0" => zeros(Nt), "tf" => zeros(Nf), "tf0" => zeros(Nf)),
Dict("reg"=>"l1","model"=>"tesm",  "src_opt" => 0, 
     "ir" => zeros(Nt), "ir0" => zeros(Nt), "tf" => zeros(Nf), "tf0" => zeros(Nf)),
Dict("reg"=>"l1","model"=>"tesm",  "src_opt" => 1, 
     "ir" => zeros(Nt), "ir0" => zeros(Nt), "tf" => zeros(Nf), "tf0" => zeros(Nf)),
]
	
for i = 1:length(results) 
	
		reg     = results[i]["reg"]
		src_opt = results[i]["src_opt"]
		model   = results[i]["model"]

		file = matopen(
		 "Data/P_Nw_$(Nw)_Nm_$(Nm)_Nt_$(Nt)_SNR_$(SNR)_$(reg)_Fs_$(Fs)_room_$(room)_srcopt$(src_opt)_$(model).mat")
		P  = read(file, "P")
		P2 = read(file, "P2")
		close(file)

		err = zeros(size(P,2))
		for l in 1:size(P,2)
			err[l] = avr_mse(P[:,l],P2[:,l])
		end

		idx_wc = indmax(err)

		results[i]["ir0"][:] =  P[:,idx_wc]./norm( P[:,idx_wc])
		results[i]["ir"][:]  = P2[:,idx_wc]./norm( P2[:,idx_wc])

		results[i]["tf0"][:] =(
			 10*log10(abs(fft(  P[:,idx_wc]./norm( P[:,idx_wc])   )))[1:Nf]
			              )
		results[i]["tf"][:]  =(
			 10*log10(abs(fft(  P2[:,idx_wc]./norm( P2[:,idx_wc])  )))[1:Nf]
			              )

end

ER = 241

figure()
cnt = 1
for i = 1:length(results)
		
	reg     = results[i]["reg"]
	src_opt = results[i]["src_opt"]
	model   = results[i]["model"]

	subplot(6,2,cnt)
	plot(t,results[i]["ir"],label = "int.")
	plot(t,results[i]["ir0"],label = "orig.")
	title("$(model) $(src_opt) $(reg)")
	subplot(6,2,cnt+1)
	plot(f,results[i]["tf"],label = "int.")
	plot(f,results[i]["tf0"],label = "orig.")
	ylim([-25;5])
	title("$(model) $(src_opt) $(reg)")
	writetable("Fig/fig/fig4_ir_$(model)_$(src_opt)_$(reg).cvs", 
	           DataFrame(t = t, ir = results[i]["ir"], ir0 = results[i]["ir0"])
		      )
	writetable("Fig/fig/fig4_tf_$(model)_$(src_opt)_$(reg).cvs", 
	           DataFrame(f = f, tf = results[i]["tf"], tf0 = results[i]["tf0"])
		      )
	println("$(results[i]["model"]) $(results[i]["src_opt"]) $(results[i]["reg"])") 
	println("wc err   : $(10*log10(avr_mse(results[i]["ir0"],results[i]["ir"]))) ")
	println("wc err ER: $(10*log10(avr_mse(results[i]["ir0"][1:ER],results[i]["ir"][1:ER]))) ")
	println("wc err LR: $(10*log10(avr_mse(results[i]["ir0"][ER:end],results[i]["ir"][ER:end]))) ")
	cnt += 2

end
#xlim([8;32])
legend()








