// Testing function envelope_corr()
//
// (Originally envolvente04.sce)
//
// ----------------------------------
// Federico Miyara
// 2006-08-15
// 2025-09-30
// 2025-10-07

clear
close(winsid())

// -------------------------------------
// Signals
// -------------------------------------

// NOTE: Replace signal files by user's files

// Envelope sample rate
Fsenv = 200;

// Duration
T = 10;

// Generate white noise normalized to a
// peak factor of 5 
Fs(1) = 44100;
x{1} = rand(1, T*Fs(1), "normal");
x{1} = x{1} / stdev(x{1}) / 5;
t{1} = (0:T*Fs(1)-1)/Fs(1);

// Load speech signal files
// Sample rates might differ as far as they
// ensure equal intelligibility conditions 
path = "S:\balanceadas\"
file1 = "semántico.wav"
[x{2}, Fs(2)] = wavread(path + file1);
x{2} = x{2}(1, 1:T*Fs(2));
t{2} = (0:T*Fs(2)-1)/Fs(2);

path = "S:\balanceadas\"
file2 = 'Memoria.wav' 
[x{3}, Fs(3)] = wavread(path + file2);
x{3} = x{3}(1, 1:T*Fs(3));
t{3} = (0:T*Fs(3)-1)/Fs(3);

path = "R:\Mis documentos\Acústica\Ruido\PSICOACÚSTICA COMPUTACIONAL\"
file3 = "Enigma (FSM).wav"
[x{4}, Fs(4)] = wavread(path + file3);
x{4} = x{4}(1, 1:T*Fs(4));
t{4} = (0:T*Fs(4)-1)/Fs(4);

// Band type: crítical
typ = 0;   

// Load function
exec  envelope_corr.sci;

// Compute envelope correlations and related
// variables (band-filtered signals, envelopes)
tic
[Xrho, fo, X, Xenv, tenv] = envelope_corr(x, Fs, typ, T, Fsenv);
toc

disp([round(fo) ..
      Xrho(1,2,:)(:) Xrho(1,3,:)(:) Xrho(1,4,:)(:) ..
      Xrho(2,3,:)(:) Xrho(2,4,:)(:) Xrho(3,4,:)(:)])

// Spectrum of the envelopes
for i = 1:length(fo)
   for n = 1:length(x)
   A{n,i} = abs(fft(Xenv{n}(i,:) - mean(Xenv{n}(i,:))));
   end
end

// Plot voice signals, one of its bands and the 
// corresponding envelope, compared to white noise
i = 1;
scf(1);
clf(1);
gcf().figure_position = [220,100];
subplot(3,2,1)
  plot(t{2}, x{2})
  title("Locución 1")
subplot(3,2,2)
  plot(t{1}, x{1})
  title("Ruido")
subplot(3,2,3)
  plot(t{2}, X{2}(i+1, :))
  title("Banda " + string(i) + ", locución 1")
subplot(3,2,4)
  plot(t{1}, X{1}(i+1, :))
  title("Banda " + string(i) + ", ruido")
subplot(3,2,5)
  plot(tenv, Xenv{2}(i+1,:))
  title("Envolvente de la banda " + string(i) + ", locución 1") 
subplot(3,2,6)
  plot(tenv, Xenv{1}(i+1,:))
  title("Envolvente de la banda " + string(i) + ", ruido")

// Plot the envelopes of a critical band of two
// utterances and noise
i = 2
scf(2);
clf(2);
gcf().figure_position = [240,80];
subplot(3,1,1)
  plot(tenv, Xenv{2}(i,1:))
  title("Locución 1, banda crítica " + string(i))
  xlabel('t  [s]')
  ylabel('Envolvente') 
subplot(3,1,2)
  plot(tenv, Xenv{3}(2,:))
  title("Locución 2, banda crítica " + string(i))
  xlabel('t  [s]')
  ylabel('Envolvente') 
subplot(3,1,3)
  plot(tenv, Xenv{1}(2,:))
  title("Ruido blanco, banda crítica " + string(i))
  xlabel('t  [s]')
  ylabel('Envolvente') 
  
// plot the spectra of the envelopes of a critical
// band of two utterances and noise
i = 2;
N = size(Xenv{1},2);
ff = (0:N/2-1)/N*Fsenv;
for n=1:length(x)
   MAX(n) = max(A{n,i})
end
scf(3);
clf(3);
gcf().figure_position = [260,60];
subplot(3,1,1)
  plot(ff, A{2,i}(1:N/2))
  title("Locución 1, espectro envolvente banda crítica " + string(i))
  xlabel("f")
  ylabel("Espectro") 
  gca().data_bounds = [0 0; 25 MAX(2)];
subplot(3,1,2)
  plot(ff, A{3,i}(1:N/2)) 
  title("Locución 2, espectro envolvente banda crítica " + string(i)) 
  xlabel("f")
  ylabel("Espectro") 
  gca().data_bounds = [0 0; 25 MAX(3)];
subplot(3,1,3)
  plot(ff, A{1,i}(1:N/2)) 
  title("Ruido blanco, espectro envolvente banda crítica " + string(i))
  xlabel("f")
  ylabel("Espectro") 
  gca().data_bounds = [0 0; 25 MAX(1)];

// Plot the spectra of the envelopes of several
// critical bands of a single utterance
scf(4);
clf(4);
gcf().figure_position = [280,40];
for i = 1:6
   subplot(3,2,i)
   plot(ff(1:N/2/4), A{1,i}(1:N/2/4)) 
   title("Espectro envolvente banda " + string(round(fo(i))) + " Hz")
end



