1% Copyright David Rowe 2009
2% This program is distributed under the terms of the GNU General Public License
3% Version 2
4%
5% Plot phase modelling information from dump files.
6
7function plphase(samname, f)
8
9  sn_name = strcat(samname,"_sn.txt");
10  Sn = load(sn_name);
11
12  sw_name = strcat(samname,"_sw.txt");
13  Sw = load(sw_name);
14
15  model_name = strcat(samname,"_model.txt");
16  model = load(model_name);
17
18  sw__name = strcat(samname,"_sw_.txt");
19  if (file_in_path(".",sw__name))
20    Sw_ = load(sw__name);
21  endif
22
23  pw_name = strcat(samname,"_pw.txt");
24  if (file_in_path(".",pw_name))
25    Pw = load(pw_name);
26  endif
27
28  ak_name = strcat(samname,"_ak.txt");
29  if (file_in_path(".",ak_name))
30    ak = load(ak_name);
31  endif
32
33  phase_name = strcat(samname,"_phase.txt");
34  if (file_in_path(".",phase_name))
35    phase = load(phase_name);
36  endif
37
38  phase_name_ = strcat(samname,"_phase_.txt");
39  if (file_in_path(".",phase_name_))
40    phase_ = load(phase_name_);
41  endif
42
43  snr_name = strcat(samname,"_snr.txt");
44  if (file_in_path(".",snr_name))
45    snr = load(snr_name);
46  endif
47
48  sn_name_ = strcat(samname,".raw");
49  if (file_in_path(".",sn_name_))
50    fs_ = fopen(sn_name_,"rb");
51    sn_  = fread(fs_,Inf,"short");
52  endif
53
54  k = ' ';
55  do
56    figure(1);
57    clf;
58    s = [ Sn(2*f-1,:) Sn(2*f,:) ];
59    plot(s);
60    grid;
61    axis([1 length(s) -20000 20000]);
62    if (k == 'p')
63       pngname = sprintf("%s_%d_sn",samname,f);
64       png(pngname);
65    endif
66
67    figure(2);
68    Wo = model(f,1);
69    L = model(f,2);
70    Am = model(f,3:(L+2));
71    plot((1:L)*Wo*4000/pi, 20*log10(Am),"r;Am;");
72    axis([1 4000 -10 80]);
73    hold on;
74    plot((0:255)*4000/256, Sw(f,:),";Sw;");
75    grid;
76
77    if (file_in_path(".",sw__name))
78      plot((0:255)*4000/256, Sw_(f,:),"g;Sw_;");
79    endif
80
81    if (file_in_path(".",pw_name))
82       plot((0:255)*4000/256, 10*log10(Pw(f,:)),";Pw;");
83    endif
84
85    if (file_in_path(".",snr_name))
86      snr_label = sprintf(";phase SNR %4.2f dB;",snr(f));
87      plot(1,1,snr_label);
88    endif
89
90    % phase model - determine SNR and error spectrum for phase model 1
91
92    if (file_in_path(".",phase_name_))
93      orig  = Am.*exp(j*phase(f,1:L));
94      synth = Am.*exp(j*phase_(f,1:L));
95      signal = orig * orig';
96      noise = (orig-synth) * (orig-synth)';
97      snr_phase = 10*log10(signal/noise);
98
99      phase_err_label = sprintf("g;phase_err SNR %4.2f dB;",snr_phase);
100      plot((1:L)*Wo*4000/pi, 20*log10(orig-synth), phase_err_label);
101    endif
102
103    hold off;
104    if (k == 'p')
105       pngname = sprintf("%s_%d_sw",samname,f);
106       png(pngname);
107    endif
108
109    if (file_in_path(".",phase_name))
110      figure(3);
111      plot((1:L)*Wo*4000/pi, phase(f,1:L)*180/pi, "-o;phase;");
112      axis;
113      if (file_in_path(".", phase_name_))
114        hold on;
115        plot((1:L)*Wo*4000/pi, phase_(f,1:L)*180/pi, "g;phase after;");
116	grid
117	hold off;
118      endif
119      if (k == 'p')
120        pngname = sprintf("%s_%d_phase",samname,f);
121        png(pngname);
122      endif
123    endif
124
125    % synthesised speech
126
127    if (file_in_path(".",sn_name_))
128      figure(4);
129      s_ = sn_((f-3)*80+1:(f+1)*80);
130      plot(s_);
131      axis([1 length(s_) -20000 20000]);
132      if (k == 'p')
133        pngname = sprintf("%s_%d_sn_",samname,f)
134        png(pngname);
135      endif
136    endif
137
138    if (file_in_path(".",ak_name))
139      figure(5);
140      axis;
141      akw = ak(f,:);
142      weight = 1.0 .^ (0:length(akw)-1);
143      akw = akw .* weight;
144      H = 1./fft(akw,8000);
145      subplot(211);
146      plot(20*log10(abs(H(1:4000))),";LPC mag spec;");
147      grid;
148      subplot(212);
149      plot(angle(H(1:4000))*180/pi,";LPC phase spec;");
150      grid;
151      if (k == 'p')
152        % stops multimode errors from gnuplot, I know not why...
153        figure(2);
154        figure(5);
155
156        pngname = sprintf("%s_%d_lpc",samname,f);
157        png(pngname);
158      endif
159    endif
160
161
162    % autocorrelation function to research voicing est
163
164    %M = length(s);
165    %sw = s .* hanning(M)';
166    %for k=0:159
167    %  R(k+1) = sw(1:320-k) * sw(1+k:320)';
168    %endfor
169    %figure(4);
170    %R_label = sprintf(";R(k) %3.2f;",max(R(20:159))/R(1));
171    %plot(R/R(1),R_label);
172    %grid
173
174    figure(2);
175
176    % interactive menu
177
178    printf("\rframe: %d  menu: n-next  b-back  p-png  q-quit ", f);
179    fflush(stdout);
180    k = kbhit();
181    if (k == 'n')
182      f = f + 1;
183    endif
184    if (k == 'b')
185      f = f - 1;
186    endif
187
188    % optional print to PNG
189
190    if (k == 'p')
191       pngname = sprintf("%s_%d",samname,f);
192       png(pngname);
193    endif
194
195  until (k == 'q')
196  printf("\n");
197
198endfunction
199