aboutsummaryrefslogtreecommitdiff
path: root/octave/codec2_demo.m
blob: 0f3950bf5d3a04eaf5e2a0f41ba8662413e1a3c0 (plain)
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
% Copyright David Rowe 2012
% This program is distributed under the terms of the GNU General Public License 
% Version 2
%
% codec2_demo.m

% Designed as an educational tool to explain the operation of Codec 2
% for conference and user group presentations on a projector.  An
% alternative to static overhead slides.
%
% Derived from codec2-dev/octave/plamp.m
%
% usage:
%   octave:1> plamp("../src/hts2a",40)
%
% Then press:
%   c - to cycle through the wavform being displayed on the figure
%   n - next frame
%   b - back one frame
%
%   tip: hold down n or b to animate the display
%
% The text files used as input are generated using c2sim:
%
%   /codec2-dev/src$ c2sim ../raw/hts2a.raw --dump hts2a
%
% The Codec 2 README explains how to build c2sim with dump files
% enabled.

function codec2_demo(samname, f)
  
  sn_name = strcat(samname,"_sn.txt");
  Sn = load(sn_name);

  sw_name = strcat(samname,"_sw.txt");
  Sw = load(sw_name);

  model_name = strcat(samname,"_model.txt");
  model = load(model_name);
  
  figure(1);

  k = ' ';
  wf = "Sn";
  do 
   
    if strcmp(wf,"Sn")
      clf;
      s = [ Sn(2*f-1,:) Sn(2*f,:) ];
      plot(s);
      axis([1 length(s) -20000 20000]);
    end

    if (strcmp(wf,"Sw"))
      clf;
      plot((0:255)*4000/256, Sw(f,:),";Sw;");
    end
  
    if strcmp(wf,"SwAm")
      Wo = model(f,1);
      L = model(f,2);
      Am = model(f,3:(L+2));
      plot((0:255)*4000/256, Sw(f,:),";Sw;");
      hold on;
      plot((1:L)*Wo*4000/pi, 20*log10(Am),"+;Am;r");
      axis([1 4000 -10 80]);
      hold off;
    end

    if strcmp(wf,"Am")
      Wo = model(f,1);
      L = model(f,2);
      Am = model(f,3:(L+2));
      plot((1:L)*Wo*4000/pi, 20*log10(Am),"+;Am;r");
      axis([1 4000 -10 80]);
    end

    % interactive menu

    printf("\rframe: %d  menu: n-next  b-back  w-cycle window  q-quit", f);
    fflush(stdout);
    k = kbhit();
    if (k == 'n')
      f = f + 1;
    end
    if (k == 'b')
      f = f - 1;
    end
    if (k == 'w') 
      if strcmp(wf,"Sn")
        next_wf = "Sw";
      end
      if strcmp(wf,"Sw")
        next_wf = "SwAm";
      end
      if strcmp(wf,"SwAm")
        next_wf = "Am";
      end
      if strcmp(wf,"Am")
        next_wf = "Sn";
      end
      wf = next_wf;
    end

  until (k == 'q')
  printf("\n");

endfunction