From: Brian West Date: Fri, 21 Dec 2012 02:08:42 +0000 (-0600) Subject: i've tested, now you can too X-Git-Tag: v1.3.13~213 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d67b96af8a2991bf159474ccc142d0765eabe732;p=thirdparty%2Ffreeswitch.git i've tested, now you can too --- diff --git a/libs/libcodec2/COPYING b/libs/libcodec2/COPYING index 4362b49151..cc40a46821 100644 --- a/libs/libcodec2/COPYING +++ b/libs/libcodec2/COPYING @@ -484,8 +484,8 @@ convey the exclusion of warranty; and each file should have at least the Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + License along with this library; if not, see + . Also add information on how to contact you by electronic and paper mail. diff --git a/libs/libcodec2/INSTALL b/libs/libcodec2/INSTALL index 23e5f25d0e..e69de29bb2 100644 --- a/libs/libcodec2/INSTALL +++ b/libs/libcodec2/INSTALL @@ -1,236 +0,0 @@ -Installation Instructions -************************* - -Copyright (C) 1994, 1995, 1996, 1999, 2000, 2001, 2002, 2004, 2005 Free -Software Foundation, Inc. - -This file is free documentation; the Free Software Foundation gives -unlimited permission to copy, distribute and modify it. - -Basic Installation -================== - -These are generic installation instructions. - - The `configure' shell script attempts to guess correct values for -various system-dependent variables used during compilation. It uses -those values to create a `Makefile' in each directory of the package. -It may also create one or more `.h' files containing system-dependent -definitions. Finally, it creates a shell script `config.status' that -you can run in the future to recreate the current configuration, and a -file `config.log' containing compiler output (useful mainly for -debugging `configure'). - - It can also use an optional file (typically called `config.cache' -and enabled with `--cache-file=config.cache' or simply `-C') that saves -the results of its tests to speed up reconfiguring. (Caching is -disabled by default to prevent problems with accidental use of stale -cache files.) - - If you need to do unusual things to compile the package, please try -to figure out how `configure' could check whether to do them, and mail -diffs or instructions to the address given in the `README' so they can -be considered for the next release. If you are using the cache, and at -some point `config.cache' contains results you don't want to keep, you -may remove or edit it. - - The file `configure.ac' (or `configure.in') is used to create -`configure' by a program called `autoconf'. You only need -`configure.ac' if you want to change it or regenerate `configure' using -a newer version of `autoconf'. - -The simplest way to compile this package is: - - 1. `cd' to the directory containing the package's source code and type - `./configure' to configure the package for your system. If you're - using `csh' on an old version of System V, you might need to type - `sh ./configure' instead to prevent `csh' from trying to execute - `configure' itself. - - Running `configure' takes awhile. While running, it prints some - messages telling which features it is checking for. - - 2. Type `make' to compile the package. - - 3. Optionally, type `make check' to run any self-tests that come with - the package. - - 4. Type `make install' to install the programs and any data files and - documentation. - - 5. You can remove the program binaries and object files from the - source code directory by typing `make clean'. To also remove the - files that `configure' created (so you can compile the package for - a different kind of computer), type `make distclean'. There is - also a `make maintainer-clean' target, but that is intended mainly - for the package's developers. If you use it, you may have to get - all sorts of other programs in order to regenerate files that came - with the distribution. - -Compilers and Options -===================== - -Some systems require unusual options for compilation or linking that the -`configure' script does not know about. Run `./configure --help' for -details on some of the pertinent environment variables. - - You can give `configure' initial values for configuration parameters -by setting variables in the command line or in the environment. Here -is an example: - - ./configure CC=c89 CFLAGS=-O2 LIBS=-lposix - - *Note Defining Variables::, for more details. - -Compiling For Multiple Architectures -==================================== - -You can compile the package for more than one kind of computer at the -same time, by placing the object files for each architecture in their -own directory. To do this, you must use a version of `make' that -supports the `VPATH' variable, such as GNU `make'. `cd' to the -directory where you want the object files and executables to go and run -the `configure' script. `configure' automatically checks for the -source code in the directory that `configure' is in and in `..'. - - If you have to use a `make' that does not support the `VPATH' -variable, you have to compile the package for one architecture at a -time in the source code directory. After you have installed the -package for one architecture, use `make distclean' before reconfiguring -for another architecture. - -Installation Names -================== - -By default, `make install' installs the package's commands under -`/usr/local/bin', include files under `/usr/local/include', etc. You -can specify an installation prefix other than `/usr/local' by giving -`configure' the option `--prefix=PREFIX'. - - You can specify separate installation prefixes for -architecture-specific files and architecture-independent files. If you -pass the option `--exec-prefix=PREFIX' to `configure', the package uses -PREFIX as the prefix for installing programs and libraries. -Documentation and other data files still use the regular prefix. - - In addition, if you use an unusual directory layout you can give -options like `--bindir=DIR' to specify different values for particular -kinds of files. Run `configure --help' for a list of the directories -you can set and what kinds of files go in them. - - If the package supports it, you can cause programs to be installed -with an extra prefix or suffix on their names by giving `configure' the -option `--program-prefix=PREFIX' or `--program-suffix=SUFFIX'. - -Optional Features -================= - -Some packages pay attention to `--enable-FEATURE' options to -`configure', where FEATURE indicates an optional part of the package. -They may also pay attention to `--with-PACKAGE' options, where PACKAGE -is something like `gnu-as' or `x' (for the X Window System). The -`README' should mention any `--enable-' and `--with-' options that the -package recognizes. - - For packages that use the X Window System, `configure' can usually -find the X include and library files automatically, but if it doesn't, -you can use the `configure' options `--x-includes=DIR' and -`--x-libraries=DIR' to specify their locations. - -Specifying the System Type -========================== - -There may be some features `configure' cannot figure out automatically, -but needs to determine by the type of machine the package will run on. -Usually, assuming the package is built to be run on the _same_ -architectures, `configure' can figure that out, but if it prints a -message saying it cannot guess the machine type, give it the -`--build=TYPE' option. TYPE can either be a short name for the system -type, such as `sun4', or a canonical name which has the form: - - CPU-COMPANY-SYSTEM - -where SYSTEM can have one of these forms: - - OS KERNEL-OS - - See the file `config.sub' for the possible values of each field. If -`config.sub' isn't included in this package, then this package doesn't -need to know the machine type. - - If you are _building_ compiler tools for cross-compiling, you should -use the option `--target=TYPE' to select the type of system they will -produce code for. - - If you want to _use_ a cross compiler, that generates code for a -platform different from the build platform, you should specify the -"host" platform (i.e., that on which the generated programs will -eventually be run) with `--host=TYPE'. - -Sharing Defaults -================ - -If you want to set default values for `configure' scripts to share, you -can create a site shell script called `config.site' that gives default -values for variables like `CC', `cache_file', and `prefix'. -`configure' looks for `PREFIX/share/config.site' if it exists, then -`PREFIX/etc/config.site' if it exists. Or, you can set the -`CONFIG_SITE' environment variable to the location of the site script. -A warning: not all `configure' scripts look for a site script. - -Defining Variables -================== - -Variables not defined in a site shell script can be set in the -environment passed to `configure'. However, some packages may run -configure again during the build, and the customized values of these -variables may be lost. In order to avoid this problem, you should set -them in the `configure' command line, using `VAR=value'. For example: - - ./configure CC=/usr/local2/bin/gcc - -causes the specified `gcc' to be used as the C compiler (unless it is -overridden in the site shell script). Here is a another example: - - /bin/bash ./configure CONFIG_SHELL=/bin/bash - -Here the `CONFIG_SHELL=/bin/bash' operand causes subsequent -configuration-related scripts to be executed by `/bin/bash'. - -`configure' Invocation -====================== - -`configure' recognizes the following options to control how it operates. - -`--help' -`-h' - Print a summary of the options to `configure', and exit. - -`--version' -`-V' - Print the version of Autoconf used to generate the `configure' - script, and exit. - -`--cache-file=FILE' - Enable the cache: use and save the results of the tests in FILE, - traditionally `config.cache'. FILE defaults to `/dev/null' to - disable caching. - -`--config-cache' -`-C' - Alias for `--cache-file=config.cache'. - -`--quiet' -`--silent' -`-q' - Do not print messages saying which checks are being made. To - suppress all normal output, redirect it to `/dev/null' (any error - messages will still be shown). - -`--srcdir=DIR' - Look for the package's source code in directory DIR. Usually - `configure' can determine that directory automatically. - -`configure' also accepts some other, not widely useful, options. Run -`configure --help' for more details. - diff --git a/libs/libcodec2/Makefile.am b/libs/libcodec2/Makefile.am index 085b8a5b1e..f4b1bfb68e 100644 --- a/libs/libcodec2/Makefile.am +++ b/libs/libcodec2/Makefile.am @@ -1,11 +1,9 @@ -AM_CFLAGS = -Isrc -Wall -lm -AUTOMAKE_OPTIONS = gnu -NAME = libcodec2 +AM_CFLAGS = -Isrc -fPIC -Wall -O3 -lm +AUTOMAKE_OPTS = gnu +NAME = codec2 AM_CPPFLAGS = $(AM_CFLAGS) -EXTRA_DIST = pitch/hts1a.p \ -pitch/hts2a.p \ -octave/glottal.m \ +EXTRA_DIST = octave/glottal.m \ octave/lsp_pdf.m \ octave/phase.m \ octave/pl2.m \ @@ -68,11 +66,6 @@ wav/hts2a_g729a.wav \ wav/m2400.wav \ wav/morig_speex_8k.wav \ src/globals.c \ -doc/A_m.gif \ -doc/omega_0.gif \ -doc/phi_m.gif \ -doc/s_n.gif \ -doc/s_n.txt \ unittest/lsp2.txt \ unittest/lsp7.txt \ unittest/lspd78.txt \ @@ -87,12 +80,15 @@ unittest/lspd123.txt \ unittest/lsp1.txt \ unittest/lsp6.txt \ unittest/lspd456.txt \ -src/codeall.sh \ -src/fq20.sh \ -src/listen1.sh \ -src/listen.sh \ -src/listensim.sh \ -src/sim.sh - +src/codebook/lsp1.txt \ +src/codebook/lsp2.txt \ +src/codebook/lsp3.txt \ +src/codebook/lsp4.txt \ +src/codebook/lsp5.txt \ +src/codebook/lsp6.txt \ +src/codebook/lsp7.txt \ +src/codebook/lsp8.txt \ +src/codebook/lsp9.txt \ +src/codebook/lsp10.txt SUBDIRS = src unittest diff --git a/libs/libcodec2/README b/libs/libcodec2/README index e69de29bb2..c83988830f 100644 --- a/libs/libcodec2/README +++ b/libs/libcodec2/README @@ -0,0 +1,84 @@ +Codec 2 README +-------------- + +Codec 2 is an open source (LGPL licensed) speech codec for 2400 bit/s +and below. For more information please see: + + http://rowetel.com/codec2.html + +Also included is a FDMDV modem, see README_fdmdv.txt + +Quickstart +---------- + +1/ Listen to Codec 2: + + $ ./configure && make + $ cd src + $ ./c2demo ../raw/hts1a.raw hts1a_c2.raw + $ ../script/menu.sh ../raw/hts1a.raw hts1a_c2.raw + + NOTE: For playback testing, menu.sh requires either the 'play', + 'aplay' or 'ossplay' programs to be installed (see + http://sox.sourceforge.net/, http://www.alsa-project.org/, or + http://www.opensound.com/ respectively). + +2/ Compress and Decompress a file: + + $ ./c2enc 2400 ../raw/hts1a.raw hts1a_c2.bit + $ ./c2dec 2400 hts1a_c2.bit hts1a_c2.raw + +3/ Same thing with pipes: + + $ ./c2enc 1400 ../raw/hts1a.raw - | ./c2dec 1400 - - | play -t raw -r 8000 -s -2 - + +Programs +-------- + +1/ c2demo encodes a file of speech samples, then decodes them and +saves the result. + +2/ c2enc encodes a file of speech samples to a compressed file of +encoded bits. + +3/ c2dec decodes a compressed file of bits to a file of speech +samples. + +4/ c2sim is a simulation/development version of Codec 2. It allows +selective use of the various Codec 2 algorithms. For example +switching phase modelling or LSP quantisation on and off. + +Debugging +--------- + +1/ For dump file support: + + $ cd codec2 + $ CFLAGS=-DDUMP ./configure + $ make clean && make + +2/ To use gdb: + + $ libtool --mode=execute gdb c2sim + +Directories +----------- + + fltk - FLTK GUI programs(s) + octave - Octave scripts used for visualising internal signals + during development + portaudio - Portaudio test programs + script - shell scripts for playing and converting raw files + src - C source code + raw - speech files in raw format (16 bits signed linear 8 kHz) + unittest - unit test source code + voicing - hand-estimated voicing files, used for development + wav - speech files in wave file format + win32 - Support for building Windows DLL version of Codec 2 and FDMDV libraries + +TODO +---- + +[ ] Get win32/Makefile integrated into Automake system, such that if + i586-mingw32msvc exists the Win32 code gets automatically built. +[ ] Same for fltk & portaudio, build these conditionally if libs exist \ No newline at end of file diff --git a/libs/libcodec2/configure.in b/libs/libcodec2/configure.in index 378ef5f2b9..8adbf2a3c8 100644 --- a/libs/libcodec2/configure.in +++ b/libs/libcodec2/configure.in @@ -2,8 +2,8 @@ # Process this file with autoconf to produce a configure script. AC_PREREQ([2.59]) -AC_INIT(libcodec2, 1.0, david@rowetel.com) -AM_INIT_AUTOMAKE(libcodec2,1.0) +AC_INIT(codec2, 0.2, david@rowetel.com) +AM_INIT_AUTOMAKE(codec2,0.2) # Checks for programs. AC_PROG_CC diff --git a/libs/libcodec2/octave/glottal.m b/libs/libcodec2/octave/glottal.m index 2b823c37e3..46675e7d6c 100644 --- a/libs/libcodec2/octave/glottal.m +++ b/libs/libcodec2/octave/glottal.m @@ -11,13 +11,17 @@ kexc = [ 8, -16, 26, -48, 86, -162, 294, -502, 718, -728, 184 672, -610, -67 kexc = shift(kexc,sh); kexc = [kexc(1:sh) zeros(1,512-25) kexc(sh+1:25)]; figure(1) +clf plot(kexc) figure(2) G = fft(kexc); +subplot(211) plot((1:256)*(4000/256),unwrap(angle(G(1:256)))) +subplot(212) +plot(20*log10(abs(G))) f=fopen("glottal.c","wt"); -fprintf(f,"float glottal[]={\n"); +fprintf(f,"const float glottal[]={\n"); for m=1:255 fprintf(f," %f,\n",angle(G(m))); endfor diff --git a/libs/libcodec2/octave/lsp_pdf.m b/libs/libcodec2/octave/lsp_pdf.m index 6617066e3d..4fc1359a36 100644 --- a/libs/libcodec2/octave/lsp_pdf.m +++ b/libs/libcodec2/octave/lsp_pdf.m @@ -7,14 +7,19 @@ function lsp_pdf(lsp) % LSPs - figure(3); + figure(1); clf; [x,y] = hist(lsp(:,1),100); - plot(y*4000/pi,x,";1;"); + plot(y*4000/pi,x,"+;1;"); hold on; - for i=2:c + for i=2:5 [x,y] = hist(lsp(:,i),100); - legend = sprintf(";%d;",i); + legend = sprintf("+%d;%d;",i,i); + plot(y*4000/pi,x,legend); + endfor + for i=6:c + [x,y] = hist(lsp(:,i),100); + legend = sprintf("+%d;%d;",i-5,i); plot(y*4000/pi,x,legend); endfor hold off; @@ -22,29 +27,65 @@ function lsp_pdf(lsp) % LSP differences - figure(4); + figure(2); clf; subplot(211) [x,y] = hist(lsp(:,1),100); - plot(y,x,";1;"); + plot(y*4000/pi,x,"1;1;"); hold on; for i=2:5 [x,y] = hist(lsp(:,i) - lsp(:,i-1),100); - legend = sprintf(";%d;",i); - plot(y,x,legend); + legend = sprintf("%d;%d;",i,i); + plot(y*4000/pi,x,legend); endfor hold off; grid; subplot(212) [x,y] = hist(lsp(:,6)-lsp(:,5),100); - plot(y,x,";6;"); + plot(y*4000/pi,x,"1;6;"); hold on; for i=7:c [x,y] = hist(lsp(:,i) - lsp(:,i-1),100); - legend = sprintf(";%d;",i); - plot(y,x,legend); + legend = sprintf("%d;%d;",i-5,i); + plot(y*4000/pi,x,legend); endfor hold off; grid; + + % LSP differences delta from last frame + + lspd(:,1) = lsp(:,1); + lspd(:,2:10) = lsp(:,2:10) - lsp(:,1:9); + + [m,n] = size(lspd); + lspdd = lspd(5:m,:) - lspd(1:m-4,:); + + figure(3); + clf; + subplot(211) + for i=1:5 + [x,y] = hist(lspdd(:,i),100); + legend = sprintf("%d;%d;",i,i); + plot(y*4000/pi,x,legend); + hold on; + endfor + hold off; + grid; + axis([-200 200 0 35000]); + + subplot(212) + for i=6:10 + [x,y] = hist(lspdd(:,i),100); + legend = sprintf("%d;%d;",i-5,i); + plot(y*4000/pi,x,legend); + hold on; + endfor + hold off; + grid; + axis([-200 200 0 16000]); + + figure(4); + clf; + plot((4000/pi)*(lsp(2:r,3)-lsp(1:r-1,3))) endfunction diff --git a/libs/libcodec2/octave/phase2.m b/libs/libcodec2/octave/phase2.m index ea58dcbe11..5c148f38d2 100644 --- a/libs/libcodec2/octave/phase2.m +++ b/libs/libcodec2/octave/phase2.m @@ -6,8 +6,8 @@ function phase2(samname, png) N = 16000; - f=45; - model = load("../src/hts1a_model.txt"); + f=43; + model = load("../src/hts1a_phase_model.txt"); phase = load("../src/hts1a_phase_phase.txt"); Wo = model(f,1); P=2*pi/Wo; @@ -15,13 +15,13 @@ function phase2(samname, png) A = model(f,3:(L+2)); phi = phase(f,1:L); phi = zeros(1,L); - for m=L/2:L - phi(m) = 2*pi*rand(1,1); - end + phi(3) = -pi/2; + phi(4) = -pi/4; + phi(5) = pi/2; s = zeros(1,N); - for m=1:L + for m=3:5 s_m = A(m)*cos(m*Wo*(0:(N-1)) + phi(m)); s = s + s_m; endfor @@ -30,6 +30,13 @@ function phase2(samname, png) clf; plot(s(1:250)); + figure(2); + clf; + subplot(211) + plot((1:L)*Wo*4000/pi, 20*log10(A),'+'); + subplot(212) + plot((1:L)*Wo*4000/pi, phi,'+'); + fs=fopen(samname,"wb"); fwrite(fs,s,"short"); fclose(fs); diff --git a/libs/libcodec2/octave/pl.m b/libs/libcodec2/octave/pl.m index 49968961d4..0d54788215 100644 --- a/libs/libcodec2/octave/pl.m +++ b/libs/libcodec2/octave/pl.m @@ -1,6 +1,9 @@ % Copyright David Rowe 2009 % This program is distributed under the terms of the GNU General Public License % Version 2 +% +% Plots a raw speech sample file, you can optionally specify the start and end +% samples and create a large and small PNGs function pl(samname1, start_sam, end_sam, pngname) @@ -19,7 +22,7 @@ function pl(samname1, start_sam, end_sam, pngname) figure(1); clf; plot(s(st:en)); - axis([1 en-st min(s) max(s)]); + axis([1 en-st 1.1*min(s) 1.1*max(s)]); if (nargin == 4) diff --git a/libs/libcodec2/octave/plamp.m b/libs/libcodec2/octave/plamp.m index 892830f032..62b6893ad5 100644 --- a/libs/libcodec2/octave/plamp.m +++ b/libs/libcodec2/octave/plamp.m @@ -4,8 +4,16 @@ % % Plot ampltiude modelling information from dump files. -function plamp(samname, f) +function plamp(samname, f, samname2) + % switch some stuff off to unclutter display + + plot_lsp = 0; + plot_snr = 0; + plot_vsnr = 0; + plot_sw = 0; + plot_pw = 0; + sn_name = strcat(samname,"_sn.txt"); Sn = load(sn_name); @@ -17,6 +25,16 @@ function plamp(samname, f) Sw_ = load(sw__name); endif + ew_name = strcat(samname,"_ew.txt"); + if (file_in_path(".",ew_name)) + Ew = load(ew_name); + endif + + rk_name = strcat(samname,"_rk.txt"); + if (file_in_path(".",rk_name)) + Rk = load(rk_name); + endif + model_name = strcat(samname,"_model.txt"); model = load(model_name); @@ -50,12 +68,24 @@ function plamp(samname, f) snr = load(snr_name); endif + % optional second file, for exploring post filter + + model2q_name = " "; + if nargin == 3 + model2q_name = strcat(samname2,"_qmodel.txt"); + if file_in_path(".",modelq_name) + model2q = load(model2q_name); + end + end + + Ew_on = 1; k = ' '; do figure(1); clf; % s = [ Sn(2*(f-2)-1,:) Sn(2*(f-2),:) ]; s = [ Sn(2*f-1,:) Sn(2*f,:) ]; + size(s); plot(s); axis([1 length(s) -20000 20000]); @@ -66,13 +96,14 @@ function plamp(samname, f) plot((1:L)*Wo*4000/pi, 20*log10(Am),";Am;r"); axis([1 4000 -10 80]); hold on; -% plot((0:255)*4000/256, Sw(f-2,:),";Sw;"); - plot((0:255)*4000/256, Sw(f,:),";Sw;"); - + if plot_sw + plot((0:255)*4000/256, Sw(f,:),";Sw;"); + end + if (file_in_path(".",modelq_name)) Amq = modelq(f,3:(L+2)); plot((1:L)*Wo*4000/pi, 20*log10(Amq),";Amq;g" ); - if (file_in_path(".",pw_name)) + if (file_in_path(".",pw_name) && plot_pw) plot((0:255)*4000/256, 10*log10(Pw(f,:)),";Pw;c"); endif signal = Am * Am'; @@ -82,8 +113,13 @@ function plamp(samname, f) plot((1:L)*Wo*4000/pi, 20*log10(Amq) - 20*log10(Am), Am_err_label); endif - if (file_in_path(".",snr_name)) - snr_label = sprintf(";phase SNR %4.2f dB;",snr(f)); + if file_in_path(".",model2q_name) + Amq2 = model2q(f,3:(L+2)); + plot((1:L)*Wo*4000/pi, 20*log10(Amq2),";Amq2;m" ); + end + + if (file_in_path(".",snr_name) && plot_vsnr) + snr_label = sprintf(";Voicing SNR %4.2f dB;",snr(f)); plot(1,1,snr_label); endif @@ -96,11 +132,11 @@ function plamp(samname, f) noise = (orig-synth) * (orig-synth)'; snr_phase = 10*log10(signal/noise); - phase_err_label = sprintf(";phase_err SNR %4.2f dB;",snr_phase); - plot((1:L)*Wo*4000/pi, 20*log10(orig-synth), phase_err_label); + %phase_err_label = sprintf(";phase_err SNR %4.2f dB;",snr_phase); + %plot((1:L)*Wo*4000/pi, 20*log10(orig-synth), phase_err_label); endif - if (file_in_path(".",lsp_name)) + if (file_in_path(".",lsp_name) && plot_lsp) for l=1:10 plot([lsp(f,l)*4000/pi lsp(f,l)*4000/pi], [60 80], 'r'); endfor @@ -108,33 +144,21 @@ function plamp(samname, f) hold off; - if (file_in_path(".",phase_name)) - figure(3); - plot((1:L)*Wo*4000/pi, phase(f,1:L), ";phase;"); - axis; - if (file_in_path(".",phase_name_)) - hold on; - plot((1:L)*Wo*4000/pi, phase_(f,1:L), ";phase_;"); - hold off; - endif - figure(2); - endif - - % autocorrelation function to research voicing est - - %M = length(s); - %sw = s .* hanning(M)'; - %for k=0:159 - % R(k+1) = sw(1:320-k) * sw(1+k:320)'; - %endfor - %figure(4); - %R_label = sprintf(";R(k) %3.2f;",max(R(20:159))/R(1)); - %plot(R/R(1),R_label); - %grid + %if (file_in_path(".",phase_name)) + %figure(3); + %plot((1:L)*Wo*4000/pi, phase(f,1:L), ";phase;"); + %axis; + %if (file_in_path(".",phase_name_)) + %hold on; + %plot((1:L)*Wo*4000/pi, phase_(f,1:L), ";phase_;"); + %hold off; + %endif + %figure(2); + %endif % interactive menu - printf("\rframe: %d menu: n-next b-back p-png q-quit ", f); + printf("\rframe: %d menu: n-next b-back p-png q-quit e-toggle Ew", f); fflush(stdout); k = kbhit(); if (k == 'n') @@ -143,6 +167,13 @@ function plamp(samname, f) if (k == 'b') f = f - 1; endif + if (k == 'e') + if (Ew_on == 1) + Ew_on = 0; + else + Ew_on = 1; + endif + endif % optional print to PNG @@ -157,7 +188,7 @@ function plamp(samname, f) pngname = sprintf("%s_%d_sw.png",samname,f); print(pngname, '-dpng', "-S500,500") pngname = sprintf("%s_%d_sw_large.png",samname,f); - print(pngname, '-dpng', "-S800,600") + print(pngname, '-dpng', "-S1200,800") endif until (k == 'q') diff --git a/libs/libcodec2/octave/plphase.m b/libs/libcodec2/octave/plphase.m index 9e61185676..c12422ea95 100644 --- a/libs/libcodec2/octave/plphase.m +++ b/libs/libcodec2/octave/plphase.m @@ -112,7 +112,7 @@ function plphase(samname, f) axis; if (file_in_path(".", phase_name_)) hold on; - plot((1:L)*Wo*4000/pi, phase_(f,1:L)*180/pi, "g;phase_;"); + plot((1:L)*Wo*4000/pi, phase_(f,1:L)*180/pi, "g;phase after;"); grid hold off; endif diff --git a/libs/libcodec2/script/menu.sh b/libs/libcodec2/script/menu.sh index 11297df9b9..c0335d2748 100755 --- a/libs/libcodec2/script/menu.sh +++ b/libs/libcodec2/script/menu.sh @@ -35,8 +35,7 @@ # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +# along with this program; if not, see . files=0 items="Q-Quit\n" @@ -52,19 +51,30 @@ do shift done -readchar=1 echo -n -e "\r" $items"- " -while [ $readchar -ne 0 ] -do +while true ; do echo -n -e "\r -" - stty cbreak # or stty raw - readchar=`dd if=/dev/tty bs=1 count=1 2>/dev/null` + stty cbreak # or stty raw. Stty uses file descriptor 0, not /dev/tty. + readchar=`dd bs=1 count=1 2>/dev/null` stty -cbreak - if [ $readchar == 'q' ] ; then - readchar=0 - fi - if [ $readchar -ne 0 ] ; then - play -r 8000 -s -2 ${file[$readchar]} $dsp 2> /dev/null + if [ -n "$readchar" ] ; then + if [ x$readchar == 'xq' -o x$readchar == 'xQ' ] ; then + echo + exit 0 + fi + if [ -z ${file[$readchar]} ] ; then + echo -n -e "\nUnknown input\n" $items"- " + continue + fi + if ( play --version ) >/dev/null 2>&1; then + play -r 8000 -s -2 ${file[$readchar]} $dsp 2> /dev/null + elif ( aplay --version ) > /dev/null 2>&1; then + aplay -r 8000 -f S16_LE ${file[$readchar]} 2> /dev/null + elif ( ossplay -f? ) > /dev/null 2>&1; then + ossplay -s8000 -fS16_LE ${file[$readchar]} 2> /dev/null + else + echo "could not find play, aplay or ossplay program" + fi fi done echo diff --git a/libs/libcodec2/src/Makefile.am b/libs/libcodec2/src/Makefile.am index e9b2fbc341..8d0d990c43 100644 --- a/libs/libcodec2/src/Makefile.am +++ b/libs/libcodec2/src/Makefile.am @@ -1,8 +1,120 @@ -AM_CFLAGS = -I../src -Wall -DFLOATING_POINT -DVAR_ARRAYS -AUTOMAKE_OPTIONS = gnu -NAME = libcodec2 +AM_CFLAGS = -I../src -fPIC -Wall -O3 -g +AUTOMAKE_OPTS = gnu +NAME = codec2 AM_CPPFLAGS = $(AM_CFLAGS) +D=codebook + +# lsp quantisers + +CODEBOOKS= \ + $D/lsp1.txt \ + $D/lsp2.txt \ + $D/lsp3.txt \ + $D/lsp4.txt \ + $D/lsp5.txt \ + $D/lsp6.txt \ + $D/lsp7.txt \ + $D/lsp8.txt \ + $D/lsp9.txt \ + $D/lsp10.txt + +# lspd quantisers + +CODEBOOKSD= \ + $D/dlsp1.txt \ + $D/dlsp2.txt \ + $D/dlsp3.txt \ + $D/dlsp4.txt \ + $D/dlsp5.txt \ + $D/dlsp6.txt \ + $D/dlsp7.txt \ + $D/dlsp8.txt \ + $D/dlsp9.txt \ + $D/dlsp10.txt + +# lspd VQ quantisers + +CODEBOOKSVQ= \ + $D/lsp1.txt \ + $D/lsp2.txt \ + $D/lsp3.txt \ + $D/lsp4.txt \ + $(top_srcdir)/unittest/lsp45678910.txt + +CODEBOOKSJND= \ + $D/lsp1.txt \ + $D/lsp2.txt \ + $D/lsp3.txt \ + $D/lsp4.txt \ + $(top_srcdir)/unittest/lspjnd5-10.txt + +CODEBOOKSDT= \ + $D/lspdt1.txt \ + $D/lspdt2.txt \ + $D/lspdt3.txt \ + $D/lspdt4.txt \ + $D/lspdt5.txt \ + $D/lspdt6.txt \ + $D/lspdt7.txt \ + $D/lspdt8.txt \ + $D/lspdt9.txt \ + $D/lspdt10.txt + +CODEBOOKSJVM= \ + $D/lspjvm1.txt \ + $D/lspjvm2.txt \ + $D/lspjvm3.txt + +CODEBOOKSVQANSSI= \ + $D/lspvqanssi1.txt \ + $D/lspvqanssi2.txt \ + $D/lspvqanssi3.txt \ + $D/lspvqanssi4.txt + +CODEBOOKSGE= \ + $D/gecb.txt + +noinst_PROGRAMS = generate_codebook genlspdtcb + +codebook.$(OBJEXT): codebook.c +codebookd.$(OBJEXT): codebookd.c +codebookdt.$(OBJEXT): codebookdt.c +codebookvq.$(OBJEXT): codebookvq.c +codebookjnd.$(OBJEXT): codebookjnd.c +codebookjvm.$(OBJEXT): codebookjvm.c +codebookvqanssi.$(OBJEXT): codebookvqanssi.c +codebookge.$(OBJEXT): codebookge.c + +codebook.lo: codebook.c + +codebook.c: generate_codebook $(CODEBOOKS) + ./generate_codebook lsp_cb $(CODEBOOKS) > codebook.c + +codebookd.c: generate_codebook $(CODEBOOKSD) + ./generate_codebook lsp_cbd $(CODEBOOKSD) > codebookd.c + +codebookdt.c: generate_codebook $(CODEBOOKSDT) + ./generate_codebook lsp_cbdt $(CODEBOOKSDT) > codebookdt.c + +codebookvq.c: generate_codebook $(CODEBOOKSVQ) + ./generate_codebook lsp_cbvq $(CODEBOOKSVQ) > codebookvq.c + +codebookjnd.c: generate_codebook $(CODEBOOKSJND) + ./generate_codebook lsp_cbjnd $(CODEBOOKSJND) > codebookjnd.c + +codebookjvm.c: generate_codebook $(CODEBOOKSJVM) + ./generate_codebook lsp_cbjvm $(CODEBOOKSJVM) > codebookjvm.c + +codebookvqanssi.c: generate_codebook $(CODEBOOKSVQANSSI) + ./generate_codebook lsp_cbvqanssi $(CODEBOOKSVQANSSI) > codebookvqanssi.c + +codebookge.c: generate_codebook $(CODEBOOKSGE) + ./generate_codebook ge_cb $(CODEBOOKSGE) > codebookge.c + +clean-local: + -rm -f codebook.c codebookd.c codebookdvq.c codebookjnd.c codebookdt.c codebookjvm.c codebookge.c codebookvqanssi.c + lib_LTLIBRARIES = libcodec2.la libcodec2_la_SOURCES = dump.c \ lpc.c \ @@ -10,44 +122,60 @@ nlp.c \ postfilter.c \ sine.c \ codec2.c \ -four1.c \ +fifo.c \ +fdmdv.c \ +kiss_fft.c \ interp.c \ lsp.c \ phase.c \ quantise.c \ pack.c \ -codebook.c +codebook.c \ +codebookd.c \ +codebookvq.c \ +codebookjnd.c \ +codebookjvm.c \ +codebookvqanssi.c \ +codebookdt.c \ +codebookge.c + libcodec2_la_CFLAGS = $(AM_CFLAGS) libcodec2_la_LDFLAGS = $(LIBS) -library_includedir = $(prefix) -library_include_HEADERS = codec2.h \ -defines.h \ -four1.h \ -interp.h \ -lsp.h \ -phase.h \ -quantise.h \ -comp.h \ -dump.h \ -globals.h \ -lpc.h \ -nlp.h \ -postfilter.h \ -sine.h \ -codebook.h - -bin_PROGRAMS = c2dec c2enc c2sim +library_includedir = $(prefix)/include +library_include_HEADERS = codec2.h -c2dec_SOURCES = c2dec.c -c2dec_LDADD = $(lib_LTLIBRARIES) -c2dec_LDFLAGS = $(LIBS) +bin_PROGRAMS = c2demo c2enc c2dec c2sim fdmdv_get_test_bits fdmdv_mod fdmdv_demod fdmdv_put_test_bits fdmdv_interleave + +c2demo_SOURCES = c2demo.c +c2demo_LDADD = $(lib_LTLIBRARIES) +c2demo_LDFLAGS = $(LIBS) c2enc_SOURCES = c2enc.c c2enc_LDADD = $(lib_LTLIBRARIES) c2enc_LDFLAGS = $(LIBS) -c2sim_SOURCES = c2sim.c +c2dec_SOURCES = c2dec.c +c2dec_LDADD = $(lib_LTLIBRARIES) +c2dec_LDFLAGS = $(LIBS) + +c2sim_SOURCES = c2sim.c ampexp.c phaseexp.c c2sim_LDADD = $(lib_LTLIBRARIES) c2sim_LDFLAGS = $(LIBS) + +fdmdv_get_test_bits_SOURCES = fdmdv_get_test_bits.c fdmdv.c kiss_fft.c +fdmdv_get_test_bits_LDFLAGS = $(LIBS) + +fdmdv_mod_SOURCES = fdmdv_mod.c fdmdv.c kiss_fft.c +fdmdv_mod_LDFLAGS = $(LIBS) + +fdmdv_demod_SOURCES = fdmdv_demod.c fdmdv.c kiss_fft.c octave.c +fdmdv_demod_LDFLAGS = $(LIBS) + +fdmdv_put_test_bits_SOURCES = fdmdv_put_test_bits.c fdmdv.c kiss_fft.c +fdmdv_put_test_bits_LDFLAGS = $(LIBS) + +fdmdv_interleave_SOURCES = fdmdv_interleave.c +fdmdv_interleave_LDFLAGS = $(LIBS) + diff --git a/libs/libcodec2/src/_kiss_fft_guts.h b/libs/libcodec2/src/_kiss_fft_guts.h new file mode 100644 index 0000000000..ba66144403 --- /dev/null +++ b/libs/libcodec2/src/_kiss_fft_guts.h @@ -0,0 +1,164 @@ +/* +Copyright (c) 2003-2010, Mark Borgerding + +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +/* kiss_fft.h + defines kiss_fft_scalar as either short or a float type + and defines + typedef struct { kiss_fft_scalar r; kiss_fft_scalar i; }kiss_fft_cpx; */ +#include "kiss_fft.h" +#include + +#define MAXFACTORS 32 +/* e.g. an fft of length 128 has 4 factors + as far as kissfft is concerned + 4*4*4*2 + */ + +struct kiss_fft_state{ + int nfft; + int inverse; + int factors[2*MAXFACTORS]; + kiss_fft_cpx twiddles[1]; +}; + +/* + Explanation of macros dealing with complex math: + + C_MUL(m,a,b) : m = a*b + C_FIXDIV( c , div ) : if a fixed point impl., c /= div. noop otherwise + C_SUB( res, a,b) : res = a - b + C_SUBFROM( res , a) : res -= a + C_ADDTO( res , a) : res += a + * */ +#ifdef FIXED_POINT +#if (FIXED_POINT==32) +# define FRACBITS 31 +# define SAMPPROD int64_t +#define SAMP_MAX 2147483647 +#else +# define FRACBITS 15 +# define SAMPPROD int32_t +#define SAMP_MAX 32767 +#endif + +#define SAMP_MIN -SAMP_MAX + +#if defined(CHECK_OVERFLOW) +# define CHECK_OVERFLOW_OP(a,op,b) \ + if ( (SAMPPROD)(a) op (SAMPPROD)(b) > SAMP_MAX || (SAMPPROD)(a) op (SAMPPROD)(b) < SAMP_MIN ) { \ + fprintf(stderr,"WARNING:overflow @ " __FILE__ "(%d): (%d " #op" %d) = %ld\n",__LINE__,(a),(b),(SAMPPROD)(a) op (SAMPPROD)(b) ); } +#endif + + +# define smul(a,b) ( (SAMPPROD)(a)*(b) ) +# define sround( x ) (kiss_fft_scalar)( ( (x) + (1<<(FRACBITS-1)) ) >> FRACBITS ) + +# define S_MUL(a,b) sround( smul(a,b) ) + +# define C_MUL(m,a,b) \ + do{ (m).r = sround( smul((a).r,(b).r) - smul((a).i,(b).i) ); \ + (m).i = sround( smul((a).r,(b).i) + smul((a).i,(b).r) ); }while(0) + +# define DIVSCALAR(x,k) \ + (x) = sround( smul( x, SAMP_MAX/k ) ) + +# define C_FIXDIV(c,div) \ + do { DIVSCALAR( (c).r , div); \ + DIVSCALAR( (c).i , div); }while (0) + +# define C_MULBYSCALAR( c, s ) \ + do{ (c).r = sround( smul( (c).r , s ) ) ;\ + (c).i = sround( smul( (c).i , s ) ) ; }while(0) + +#else /* not FIXED_POINT*/ + +# define S_MUL(a,b) ( (a)*(b) ) +#define C_MUL(m,a,b) \ + do{ (m).r = (a).r*(b).r - (a).i*(b).i;\ + (m).i = (a).r*(b).i + (a).i*(b).r; }while(0) +# define C_FIXDIV(c,div) /* NOOP */ +# define C_MULBYSCALAR( c, s ) \ + do{ (c).r *= (s);\ + (c).i *= (s); }while(0) +#endif + +#ifndef CHECK_OVERFLOW_OP +# define CHECK_OVERFLOW_OP(a,op,b) /* noop */ +#endif + +#define C_ADD( res, a,b)\ + do { \ + CHECK_OVERFLOW_OP((a).r,+,(b).r)\ + CHECK_OVERFLOW_OP((a).i,+,(b).i)\ + (res).r=(a).r+(b).r; (res).i=(a).i+(b).i; \ + }while(0) +#define C_SUB( res, a,b)\ + do { \ + CHECK_OVERFLOW_OP((a).r,-,(b).r)\ + CHECK_OVERFLOW_OP((a).i,-,(b).i)\ + (res).r=(a).r-(b).r; (res).i=(a).i-(b).i; \ + }while(0) +#define C_ADDTO( res , a)\ + do { \ + CHECK_OVERFLOW_OP((res).r,+,(a).r)\ + CHECK_OVERFLOW_OP((res).i,+,(a).i)\ + (res).r += (a).r; (res).i += (a).i;\ + }while(0) + +#define C_SUBFROM( res , a)\ + do {\ + CHECK_OVERFLOW_OP((res).r,-,(a).r)\ + CHECK_OVERFLOW_OP((res).i,-,(a).i)\ + (res).r -= (a).r; (res).i -= (a).i; \ + }while(0) + + +#ifdef FIXED_POINT +# define KISS_FFT_COS(phase) floor(.5+SAMP_MAX * cos (phase)) +# define KISS_FFT_SIN(phase) floor(.5+SAMP_MAX * sin (phase)) +# define HALF_OF(x) ((x)>>1) +#elif defined(USE_SIMD) +# define KISS_FFT_COS(phase) _mm_set1_ps( cos(phase) ) +# define KISS_FFT_SIN(phase) _mm_set1_ps( sin(phase) ) +# define HALF_OF(x) ((x)*_mm_set1_ps(.5)) +#else +# define KISS_FFT_COS(phase) (kiss_fft_scalar) cos(phase) +# define KISS_FFT_SIN(phase) (kiss_fft_scalar) sin(phase) +# define HALF_OF(x) ((x)*.5) +#endif + +#define kf_cexp(x,phase) \ + do{ \ + (x)->r = KISS_FFT_COS(phase);\ + (x)->i = KISS_FFT_SIN(phase);\ + }while(0) + + +/* a debugging function */ +#define pcpx(c)\ + fprintf(stderr,"%g + %gi\n",(double)((c)->r),(double)((c)->i) ) + + +#ifdef KISS_FFT_USE_ALLOCA +// define this to allow use of alloca instead of malloc for temporary buffers +// Temporary buffers are used in two case: +// 1. FFT sizes that have "bad" factors. i.e. not 2,3 and 5 +// 2. "in-place" FFTs. Notice the quotes, since kissfft does not really do an in-place transform. +#include +#define KISS_FFT_TMP_ALLOC(nbytes) alloca(nbytes) +#define KISS_FFT_TMP_FREE(ptr) +#else +#define KISS_FFT_TMP_ALLOC(nbytes) KISS_FFT_MALLOC(nbytes) +#define KISS_FFT_TMP_FREE(ptr) KISS_FFT_FREE(ptr) +#endif diff --git a/libs/libcodec2/src/ampexp.c b/libs/libcodec2/src/ampexp.c new file mode 100644 index 0000000000..4d3b70a854 --- /dev/null +++ b/libs/libcodec2/src/ampexp.c @@ -0,0 +1,1093 @@ +/*---------------------------------------------------------------------------*\ + + FILE........: ampexp.c + AUTHOR......: David Rowe + DATE CREATED: 7 August 2012 + + Functions for experimenting with amplitude quantisation. + +\*---------------------------------------------------------------------------*/ + +/* + Copyright (C) 2012 David Rowe + + All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 2.1, as + published by the Free Software Foundation. This program is + distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program; if not,see . +*/ + + +#include +#include +#include +#include +#include +#include + +#include "ampexp.h" + + +#define PRED_COEFF 0.9 + +/* states for amplitude experiments */ + +struct codebook { + unsigned int k; + unsigned int log2m; + unsigned int m; + float *cb; + unsigned int offset; +}; + +struct AEXP { + float A_prev[MAX_AMP]; + int frames; + float snr; + int snr_n; + float var; + int var_n; + float vq_var; + int vq_var_n; + struct codebook *vq1,*vq2,*vq3,*vq4,*vq5; + + int indexes[5][3]; + MODEL model[3]; + float mag[3]; + MODEL model_uq[3]; +}; + + +/*---------------------------------------------------------------------------*\ + + Bruce Perens' funcs to load codebook files + +\*---------------------------------------------------------------------------*/ + + +static const char format[] = +"The table format must be:\n" +"\tTwo integers describing the dimensions of the codebook.\n" +"\tThen, enough numbers to fill the specified dimensions.\n"; + +static float get_float(FILE * in, const char * name, char * * cursor, char * buffer, int size) +{ + for ( ; ; ) { + char * s = *cursor; + char c; + + while ( (c = *s) != '\0' && !isdigit(c) && c != '-' && c != '.' ) + s++; + + /* Comments start with "#" and continue to the end of the line. */ + if ( c != '\0' && c != '#' ) { + char * end = 0; + float f = 0; + + f = strtod(s, &end); + + if ( end != s ) + *cursor = end; + return f; + } + + if ( fgets(buffer, size, in) == NULL ) { + fprintf(stderr, "%s: Format error. %s\n", name, format); + exit(1); + } + *cursor = buffer; + } +} + +static struct codebook *load(const char * name) +{ + FILE *file; + char line[2048]; + char *cursor = line; + struct codebook *b = malloc(sizeof(struct codebook)); + int i; + int size; + + file = fopen(name, "rt"); + assert(file != NULL); + + *cursor = '\0'; + + b->k = (int)get_float(file, name, &cursor, line, sizeof(line)); + b->m = (int)get_float(file, name ,&cursor, line, sizeof(line)); + size = b->k * b->m; + + b->cb = (float *)malloc(size * sizeof(float)); + + for ( i = 0; i < size; i++ ) { + b->cb[i] = get_float(file, name, &cursor, line, sizeof(line)); + } + + fclose(file); + + return b; +} + + +/*---------------------------------------------------------------------------* \ + + amp_experiment_create() + + Inits states for amplitude quantisation experiments. + +\*---------------------------------------------------------------------------*/ + +struct AEXP *amp_experiment_create() { + struct AEXP *aexp; + int i,j,m; + + aexp = (struct AEXP *)malloc(sizeof(struct AEXP)); + assert (aexp != NULL); + + for(i=0; iA_prev[i] = 1.0; + aexp->frames = 0; + aexp->snr = 0.0; + aexp->snr_n = 0; + aexp->var = 0.0; + aexp->var_n = 0; + aexp->vq_var = 0.0; + aexp->vq_var_n = 0; + + //aexp->vq1 = load("amp_1_80_1024a.txt"); + //aexp->vq1 = load("../unittest/st1_10_1024.txt"); + //aexp->vq1 = load("../unittest/amp41_80_1024.txt"); + //aexp->vq1->offset = 40; + aexp->vq1 = load("../unittest/amp1_10_1024.txt"); + aexp->vq1->offset = 0; + aexp->vq2 = load("../unittest/amp11_20_1024.txt"); + aexp->vq2->offset = 10; + + aexp->vq3 = load("../unittest/amp21_40_1024.txt"); + aexp->vq3->offset = 20; + aexp->vq4 = load("../unittest/amp41_60_1024.txt"); + aexp->vq4->offset = 40; + aexp->vq5 = load("../unittest/amp61_80_256.txt"); + aexp->vq5->offset = 60; + + #ifdef CAND2_GS + //aexp->vq1 = load("../unittest/t1_amp1_20_1024.txt"); + //aexp->vq1 = load("../unittest/t2_amp1_20_1024.txt"); + aexp->vq1 = load("../unittest/amp1_20_1024.txt"); + aexp->vq1->offset = 0; + aexp->vq2 = load("../unittest/amp21_40_1024.txt"); + aexp->vq2->offset = 20; + aexp->vq3 = load("../unittest/amp41_60_1024.txt"); + aexp->vq3->offset = 40; + aexp->vq4 = load("../unittest/amp61_80_32.txt"); + aexp->vq4->offset = 60; + #endif + + //#define CAND2_GS + #ifdef CAND2_GS + aexp->vq1 = load("../unittest/amp1_20_1024.txt"); + aexp->vq2 = load("../unittest/amp21_40_1024.txt"); + aexp->vq3 = load("../unittest/amp41_80_1024.txt"); + aexp->vq4 = load("../unittest/amp61_80_32.txt"); + aexp->vq1->offset = 0; + aexp->vq2->offset = 20; + aexp->vq3->offset = 40; + aexp->vq4->offset = 60; + #endif + + //#define CAND1 + #ifdef CAND1 + aexp->vq1 = load("../unittest/amp1_10_128.txt"); + aexp->vq2 = load("../unittest/amp11_20_512.txt"); + aexp->vq3 = load("../unittest/amp21_40_1024.txt"); + aexp->vq4 = load("../unittest/amp41_60_1024.txt"); + aexp->vq5 = load("../unittest/amp61_80_32.txt"); + aexp->vq1->offset = 0; + aexp->vq2->offset = 10; + aexp->vq3->offset = 20; + aexp->vq4->offset = 40; + aexp->vq5->offset = 60; + #endif + + for(i=0; i<3; i++) { + for(j=0; j<5; j++) + aexp->indexes[j][i] = 0; + aexp->mag[i] = 1.0; + aexp->model[i].Wo = TWO_PI*100.0/8000.0; + aexp->model[i].L = floor(PI/aexp->model[i].Wo); + for(m=1; m<=MAX_AMP; m++) + aexp->model[i].A[m] = 10.0; + aexp->model_uq[i] = aexp->model[i]; + } + + return aexp; +} + + +/*---------------------------------------------------------------------------* \ + + amp_experiment_destroy() + +\*---------------------------------------------------------------------------*/ + +void amp_experiment_destroy(struct AEXP *aexp) { + assert(aexp != NULL); + if (aexp->snr != 0.0) + printf("snr: %4.2f dB\n", aexp->snr/aexp->snr_n); + if (aexp->var != 0.0) + printf("var...: %4.3f std dev...: %4.3f (%d amplitude samples)\n", + aexp->var/aexp->var_n, sqrt(aexp->var/aexp->var_n), aexp->var_n); + if (aexp->vq_var != 0.0) + printf("vq var: %4.3f std dev...: %4.3f (%d amplitude samples)\n", + aexp->vq_var/aexp->vq_var_n, sqrt(aexp->vq_var/aexp->vq_var_n), aexp->vq_var_n); + free(aexp); +} + + +/*---------------------------------------------------------------------------*\ + + Various test and experimental functions ................ + +\*---------------------------------------------------------------------------*/ + +/* + Quantisation noise simulation. Assume noise on amplitudes is a uniform + distribution, of +/- x dB. This means x = sqrt(3)*sigma. + + Note: for uniform distribution var = = sigma * sigma = (b-a)*(b-a)/12. +*/ + +static void add_quant_noise(struct AEXP *aexp, MODEL *model, int start, int end, float sigma_dB) +{ + int m; + float x_dB; + float noise_sam_dB; + float noise_sam_lin; + + x_dB = sqrt(3.0) * sigma_dB; + + for(m=start; m<=end; m++) { + noise_sam_dB = x_dB*(1.0 - 2.0*rand()/RAND_MAX); + //printf("%f\n", noise_sam_dB); + noise_sam_lin = pow(10.0, noise_sam_dB/20.0); + model->A[m] *= noise_sam_lin; + aexp->var += noise_sam_dB*noise_sam_dB; + aexp->var_n++; + } + +} + +/* + void print_sparse_pred_error() + + use to check pred error stats (e.g. of first 1kHz) in Octave: + + $ ./c2sim ../raw/hts1a.raw --ampexp > amppe.txt + + octave> load ../src/amppe.txt + octave> std(nonzeros(amppe(:,1:20))) + octave> hist(nonzeros(amppe(:,1:20)),20); + + */ + + +static void print_sparse_pred_error(struct AEXP *aexp, MODEL *model, float mag_thresh) +{ + int m, index; + float mag, error; + float sparse_pe[MAX_AMP]; + + mag = 0.0; + for(m=1; m<=model->L; m++) + mag += model->A[m]*model->A[m]; + mag = 10*log10(mag/model->L); + + if (mag > mag_thresh) { + for(m=0; mL; m++) { + assert(model->A[m] > 0.0); + error = PRED_COEFF*20.0*log10(aexp->A_prev[m]) - 20.0*log10(model->A[m]); + //error = 20.0*log10(model->A[m]) - mag; + + index = MAX_AMP*m*model->Wo/PI; + assert(index < MAX_AMP); + sparse_pe[index] = error; + } + + /* dump sparse amp vector */ + + for(m=0; mL; m++) + e += model->A[m]*model->A[m]; + edB = 10*log10(e); + + #define VER_E0 + + #ifdef VER_E0 + *enormdB = 10*log10(e/model->L); /* make high and low pitches have similar amps */ + #endif + + #ifdef VER_E1 + e = 0.0; + for(m=1; m<=model->L; m++) + e += 10*log10(model->A[m]*model->A[m]); + *enormdB = e; + #endif + + #ifdef VER_E2 + e = 0.0; + for(m=1; m<=model->L; m++) + e += 10*log10(model->A[m]*model->A[m]); + *enormdB = e/model->L; + #endif + //printf("%f\n", enormdB); + + return edB; +} + +static void print_sparse_amp_error(struct AEXP *aexp, MODEL *model, float edB_thresh) +{ + int m, index; + float edB, enormdB, error, dWo, Am; + float sparse_pe[MAX_AMP]; + + edB = frame_energy(model, &enormdB); + //printf("%f\n", enormdB); + dWo = fabs((aexp->model_uq[2].Wo - aexp->model_uq[1].Wo)/aexp->model_uq[2].Wo); + + if ((edB > edB_thresh) && (dWo < 0.1)) { + for(m=0; mL; m++) { + assert(model->A[m] > 0.0); + error = 20.0*log10(model->A[m]) - enormdB; + + index = MAX_AMP*m*model->Wo/PI; + assert(index < MAX_AMP); + sparse_pe[index] = error; + } + + /* dump sparse amp vector */ + + for(m=0; mcb, &sparse_pe_in[vq->offset], &weights[vq->offset], vq->k, vq->m, &se); + printf("\n offset %d k %d m %d vq_ind %d j: ", vq->offset, vq->k, vq->m, vq_ind); + + non_zero = 0; + for(i=0, j=vq->offset; ik; i++,j++) { + if (sparse_pe_in[j] != 0.0) { + printf("%d ", j); + sparse_pe_in[j] -= vq->cb[vq->k * vq_ind + i]; + sparse_pe_out[j] += vq->cb[vq->k * vq_ind + i]; + non_zero++; + } + } + aexp->vq_var_n += non_zero; + return vq_ind; +} + + +static void sparse_vq_pred_error(struct AEXP *aexp, + MODEL *model +) +{ + int m, index; + float error, amp_dB, edB, enormdB; + float sparse_pe_in[MAX_AMP]; + float sparse_pe_out[MAX_AMP]; + float weights[MAX_AMP]; + + edB = frame_energy(model, &enormdB); + + for(m=0; mL; m++) { + assert(model->A[m] > 0.0); + error = PRED_COEFF*20.0*log10(aexp->A_prev[m]) - 20.0*log10(model->A[m]); + + index = MAX_AMP*m*model->Wo/PI; + assert(index < MAX_AMP); + sparse_pe_in[index] = error; + weights[index] = model->A[m]; + } + + /* vector quantise */ + + for(m=0; mvq1, weights, sparse_pe_in); + #else + for(m=aexp->vq->offset; mvq->offset+aexp->vq->k; m++) { + if (sparse_pe_in[m] != 0.0) { + float error = 8*(1.0 - 2.0*rand()/RAND_MAX); + aexp->vq_var += error*error; + aexp->vq_var_n++; + sparse_pe_out[m] = sparse_pe_in[m] + error; + } + } + #endif + + if (edB > -100.0) + for(m=0; mvq_var += pow(sparse_pe_out[m] - sparse_pe_in[m], 2.0); + } + + /* transform quantised amps back */ + + for(m=1; m<=model->L; m++) { + index = MAX_AMP*m*model->Wo/PI; + assert(index < MAX_AMP); + amp_dB = PRED_COEFF*20.0*log10(aexp->A_prev[m]) - sparse_pe_out[index]; + //printf("in: %f out: %f\n", sparse_pe_in[index], sparse_pe_out[index]); + //printf("amp_dB: %f A[m] (dB) %f\n", amp_dB, 20.0*log10(model->A[m])); + model->A[m] = pow(10.0, amp_dB/20.0); + } + //exit(0); +} + + +static void split_error(struct AEXP *aexp, struct codebook *vq, float sparse_pe_in[], int ind) +{ + int i, j; + + for(i=0, j=vq->offset; ik; i++,j++) { + if (sparse_pe_in[j] != 0.0) { + sparse_pe_in[j] -= vq->cb[vq->k * ind + i]; + } + } +} + + +static void sparse_vq_amp(struct AEXP *aexp, MODEL *model) +{ + int m, index; + float error, amp_dB, edB, enormdB; + float sparse_pe_in[MAX_AMP]; + float sparse_pe_out[MAX_AMP]; + float weights[MAX_AMP]; + + edB = frame_energy(model, &enormdB); + + aexp->mag[2] = enormdB; + + for(m=0; mL; m++) { + assert(model->A[m] > 0.0); + error = 20.0*log10(model->A[m]) - enormdB; + + index = MAX_AMP*m*model->Wo/PI; + assert(index < MAX_AMP); + sparse_pe_in[index] = error; + weights[index] = pow(model->A[m],0.8); + } + + /* vector quantise */ + + for(m=0; mindexes[0][2] = split_vq(sparse_pe_out, aexp, aexp->vq1, weights, sparse_pe_in); + + aexp->indexes[1][2] = split_vq(sparse_pe_out, aexp, aexp->vq2, weights, sparse_pe_in); + aexp->indexes[2][2] = split_vq(sparse_pe_out, aexp, aexp->vq3, weights, sparse_pe_in); + aexp->indexes[3][2] = split_vq(sparse_pe_out, aexp, aexp->vq4, weights, sparse_pe_in); + aexp->indexes[4][2] = split_vq(sparse_pe_out, aexp, aexp->vq5, weights, sparse_pe_in); + #endif + //#define MULTISTAGE + #ifdef MULTISTAGE + aexp->indexes[0][2] = split_vq(sparse_pe_out, aexp, aexp->vq1, weights, sparse_pe_in); + aexp->indexes[1][2] = split_vq(sparse_pe_out, aexp, aexp->vq2, weights, sparse_pe_in); + aexp->indexes[2][2] = split_vq(sparse_pe_out, aexp, aexp->vq3, weights, sparse_pe_in); + //aexp->indexes[3][2] = split_vq(sparse_pe_out, aexp, aexp->vq4, weights, sparse_pe_in); + #endif + + for(m=0; mvq_var += pow(sparse_pe_out[m] - sparse_pe_in[m], 2.0); + } + + /* transform quantised amps back */ + + for(m=1; m<=model->L; m++) { + index = MAX_AMP*m*model->Wo/PI; + assert(index < MAX_AMP); + amp_dB = sparse_pe_out[index] + enormdB; + model->A[m] = pow(10.0, amp_dB/20.0); + } + //exit(0); +} + + +static void update_snr_calc(struct AEXP *aexp, MODEL *m1, MODEL *m2) +{ + int m; + float signal, noise, signal_dB; + + assert(m1->L == m2->L); + + signal = 0.0; noise = 1E-32; + for(m=1; m<=m1->L; m++) { + signal += m1->A[m]*m1->A[m]; + noise += pow(m1->A[m] - m2->A[m], 2.0); + //printf("%f %f\n", before[m], model->phi[m]); + } + signal_dB = 10*log10(signal); + if (signal_dB > -100.0) { + aexp->snr += 10.0*log10(signal/noise); + aexp->snr_n++; + } +} + + +/* gain/shape vq search. Returns index of best gain. Gain is additive (as we use log quantisers) */ + +int gain_shape_vq_amp(float cb[], float vec[], float weights[], int d, int e, float *se, float *best_gain) +{ + float error; /* current error */ + int besti; /* best index so far */ + float best_error; /* best error so far */ + int i,j,m; + float diff, metric, best_metric, gain, sumAm, sumCb; + + besti = 0; + best_metric = best_error = 1E32; + for(j=0; jcb, &sparse_pe_in[vq->offset], &weights[vq->offset], vq->k, vq->m, &se, best_gain); + //printf("\n offset %d k %d m %d vq_ind %d gain: %4.2f j: ", vq->offset, vq->k, vq->m, vq_ind, *best_gain); + + non_zero = 0; + for(i=0, j=vq->offset; ik; i++,j++) { + if (sparse_pe_in[j] != 0.0) { + //printf("%d ", j); + sparse_pe_out[j] = vq->cb[vq->k * vq_ind + i] + *best_gain; + non_zero++; + } + } + aexp->vq_var_n += non_zero; +} + + +static void gain_shape_sparse_vq_amp(struct AEXP *aexp, MODEL *model) +{ + int m, index; + float amp_dB, best_gain; + float sparse_pe_in[MAX_AMP]; + float sparse_pe_out[MAX_AMP]; + float weights[MAX_AMP]; + + for(m=0; mL; m++) { + assert(model->A[m] > 0.0); + + index = MAX_AMP*m*model->Wo/PI; + assert(index < MAX_AMP); + sparse_pe_in[index] = 20.0*log10(model->A[m]); + weights[index] = model->A[m]; + } + + /* vector quantise */ + + for(m=0; m<=MAX_AMP; m++) { + sparse_pe_out[m] = sparse_pe_in[m]; + } + + gain_shape_split_vq(sparse_pe_out, aexp, aexp->vq1, weights, sparse_pe_in, &best_gain); + gain_shape_split_vq(sparse_pe_out, aexp, aexp->vq2, weights, sparse_pe_in, &best_gain); + gain_shape_split_vq(sparse_pe_out, aexp, aexp->vq3, weights, sparse_pe_in, &best_gain); + gain_shape_split_vq(sparse_pe_out, aexp, aexp->vq4, weights, sparse_pe_in, &best_gain); + + for(m=0; mvq_var += pow(sparse_pe_out[m] - sparse_pe_in[m], 2.0); + } + + /* transform quantised amps back */ + + for(m=1; m<=model->L; m++) { + index = MAX_AMP*m*model->Wo/PI; + assert(index < MAX_AMP); + amp_dB = sparse_pe_out[index]; + model->A[m] = pow(10.0, amp_dB/20.0); + } + //exit(0); +} + + +static void interp_split_vq(float sparse_pe_out[], struct AEXP *aexp, struct codebook *vq, float sparse_pe_in[], int ind) +{ + int i, j; + float amp_dB; + + for(i=0, j=vq->offset; ik; i++,j++) { + if (sparse_pe_in[j] != 0.0) { + amp_dB = 0.5*(aexp->mag[0] + vq->cb[vq->k * aexp->indexes[ind][0] + i]); + amp_dB += 0.5*(aexp->mag[2] + vq->cb[vq->k * aexp->indexes[ind][2] + i]); + sparse_pe_out[j] = amp_dB; + } + } +} + + +static void vq_interp(struct AEXP *aexp, MODEL *model, int on) +{ + int i, j, m, index; + float amp_dB; + //struct codebook *vq = aexp->vq1; + float sparse_pe_in[MAX_AMP]; + float sparse_pe_out[MAX_AMP]; + + /* replace odd frames with interp */ + /* once we get an even input frame we can interpolate and output odd */ + /* using VQ to interpolate. This assumes some correlation in + adjacent VQ samples */ + + memcpy(&aexp->model[2], model, sizeof(MODEL)); + + /* once we get an even input frame we have enough information to + replace prev odd frame with interpolated version */ + + if (on && ((aexp->frames % 2) == 0)) { + + /* copy Wo, L, and phases */ + + memcpy(model, &aexp->model[1], sizeof(MODEL)); + //printf("mags: %4.2f %4.2f %4.2f Am: \n", aexp->mag[0], aexp->mag[1], aexp->mag[2]); + + /* now replace Am by interpolation, use similar design to VQ + to handle different bands */ + + for(m=1; m<=model->L; m++) { + assert(model->A[m] > 0.0); + + index = MAX_AMP*m*model->Wo/PI; + assert(index < MAX_AMP); + sparse_pe_in[index] = 20.0*log10(model->A[m]); + } + + /* this can be used for when just testing partial interpolation */ + + for(m=0; mvq1, sparse_pe_in, 0); + interp_split_vq(sparse_pe_out, aexp, aexp->vq2, sparse_pe_in, 1); + interp_split_vq(sparse_pe_out, aexp, aexp->vq3, sparse_pe_in, 2); + interp_split_vq(sparse_pe_out, aexp, aexp->vq4, sparse_pe_in, 3); + interp_split_vq(sparse_pe_out, aexp, aexp->vq5, sparse_pe_in, 4); + + for(m=1; m<=model->L; m++) { + index = MAX_AMP*m*model->Wo/PI; + assert(index < MAX_AMP); + amp_dB = sparse_pe_out[index]; + //printf(" %4.2f", 10.0*log10(model->A[m])); + model->A[m] = pow(10.0, amp_dB/20.0); + //printf(" %4.2f\n", 10.0*log10(model->A[m])); + } + + #ifdef INITIAL_VER + + for(m=1; m<=model->L; m++) { + index = MAX_AMP*m*model->Wo/PI; + assert(index < MAX_AMP); + + if (index < vq->k) { + amp_dB = 0.5*(aexp->mag[0] + vq->cb[vq->k * aexp->indexes[0] + index]); + amp_dB += 0.5*(aexp->mag[2] + vq->cb[vq->k * aexp->indexes[2] + index]); + //printf(" %4.2f", 10.0*log10(model->A[m])); + //amp_dB = 10; + model->A[m] = pow(10.0, amp_dB/20.0); + printf(" %4.2f\n", 10.0*log10(model->A[m])); + } + } + + #endif + } + else + memcpy(model, &aexp->model[1], sizeof(MODEL)); + + /* update memories */ + + for(i=0; i<2; i++) { + memcpy(&aexp->model[i], &aexp->model[i+1], sizeof(MODEL)); + for(j=0; j<5; j++) + aexp->indexes[j][i] = aexp->indexes[j][i+1]; + aexp->mag[i] = aexp->mag[i+1]; + } + +} + + +/* + This functions tests theory that some bands can be combined together + due to less frequency resolution at higher frequencies. This will + reduce the amount of information we need to encode. +*/ + +void smooth_samples(struct AEXP *aexp, MODEL *model, int mode) +{ + int m, i, j, index, step, nav, v, en; + float sparse_pe_in[MAX_AMP], av, amp_dB; + float sparse_pe_out[MAX_AMP]; + float smoothed[MAX_AMP], smoothed_out[MAX_AMP]; + float weights[MAX_AMP]; + float edB, enormdB; + + edB = frame_energy(model, &enormdB); + + for(m=0; mL; m++) { + assert(model->A[m] > 0.0); + + index = MAX_AMP*m*model->Wo/PI; + assert(index < MAX_AMP); + sparse_pe_out[index] = sparse_pe_in[index] = 20.0*log10(model->A[m]) - enormdB; + } + + /* now combine samples at high frequencies to reduce dimension */ + + step=4; + for(i=MAX_AMP/2,v=0; i (MAX_AMP-1)) + en = MAX_AMP-1; + for(j=i; jvq1, weights, smoothed); + for(i=0; i (MAX_AMP-1)) + en = MAX_AMP-1; + for(j=i; jL; m++) { + index = MAX_AMP*m*model->Wo/PI; + assert(index < MAX_AMP); + amp_dB = sparse_pe_out[index] + enormdB; + //printf("%d %4.2f %4.2f\n", m, 10.0*log10(model->A[m]), amp_dB); + model->A[m] = pow(10.0, amp_dB/20.0); + } + +} + +#define MAX_BINS 40 +static float bins[] = { + /*1000.0, 1200.0, 1400.0, 1600.0, 1800,*/ + 2000.0, 2400.0, 2800.0, + 3000.0, 3400.0, 3600.0, 4000.0}; + +void smooth_amp(struct AEXP *aexp, MODEL *model) { + int m, i; + int nbins; + int b; + float f; + float av[MAX_BINS]; + int nav[MAX_BINS]; + + nbins = sizeof(bins)/sizeof(float); + + /* clear all bins */ + + for(i=0; iL; m++) { + f = m*model->Wo*FS/TWO_PI; + if (f > bins[0]) { + + /* find bin */ + + for(i=0; i bins[i]) && (f <= bins[i+1])) + b = i; + assert(b < MAX_BINS); + + av[b] += model->A[m]*model->A[m]; + nav[b]++; + } + + } + + /* use averages to est amps */ + + for(m=1; m<=model->L; m++) { + f = m*model->Wo*FS/TWO_PI; + if (f > bins[0]) { + + /* find bin */ + + for(i=0; i bins[i]) && (f <= bins[i+1])) + b = i; + assert(b < MAX_BINS); + + /* add predicted phase error to this bin */ + + printf("L %d m %d f %4.f b %d\n", model->L, m, f, b); + + printf(" %d: %4.3f -> ", m, 20*log10(model->A[m])); + model->A[m] = sqrt(av[b]/nav[b]); + printf("%4.3f\n", 20*log10(model->A[m])); + } + } + printf("\n"); +} + +/*---------------------------------------------------------------------------* \ + + amp_experiment() + + Amplitude quantisation experiments. + +\*---------------------------------------------------------------------------*/ + +void amp_experiment(struct AEXP *aexp, MODEL *model, char *arg) { + int m,i; + + memcpy(&aexp->model_uq[2], model, sizeof(MODEL)); + + if (strcmp(arg, "qn") == 0) { + add_quant_noise(aexp, model, 1, model->L, 1); + update_snr_calc(aexp, &aexp->model_uq[2], model); + } + + /* print training samples that can be > train.txt for training VQ */ + + if (strcmp(arg, "train") == 0) + print_sparse_amp_error(aexp, model, 00.0); + + /* VQ of amplitudes, no interpolation (ie 10ms rate) */ + + if (strcmp(arg, "vq") == 0) { + sparse_vq_amp(aexp, model); + vq_interp(aexp, model, 0); + update_snr_calc(aexp, &aexp->model_uq[1], model); + } + + /* VQ of amplitudes, interpolation (ie 20ms rate) */ + + if (strcmp(arg, "vqi") == 0) { + sparse_vq_amp(aexp, model); + vq_interp(aexp, model, 1); + update_snr_calc(aexp, &aexp->model_uq[1], model); + } + + /* gain/shape VQ of amplitudes, 10ms rate (doesn't work that well) */ + + if (strcmp(arg, "gsvq") == 0) { + gain_shape_sparse_vq_amp(aexp, model); + vq_interp(aexp, model, 0); + update_snr_calc(aexp, &aexp->model_uq[1], model); + } + + if (strcmp(arg, "smooth") == 0) { + smooth_samples(aexp, model, 0); + update_snr_calc(aexp, &aexp->model_uq[2], model); + } + + if (strcmp(arg, "smoothtrain") == 0) { + smooth_samples(aexp, model, 1); + //update_snr_calc(aexp, &aexp->model_uq[2], model); + } + + if (strcmp(arg, "smoothvq") == 0) { + smooth_samples(aexp, model, 2); + update_snr_calc(aexp, &aexp->model_uq[2], model); + } + + if (strcmp(arg, "smoothamp") == 0) { + smooth_amp(aexp, model); + update_snr_calc(aexp, &aexp->model_uq[2], model); + } + + /* update states */ + + for(m=1; m<=model->L; m++) + aexp->A_prev[m] = model->A[m]; + aexp->frames++; + for(i=0; i<3; i++) + aexp->model_uq[i] = aexp->model_uq[i+1]; +} + diff --git a/libs/libcodec2/src/ampexp.h b/libs/libcodec2/src/ampexp.h new file mode 100644 index 0000000000..313abb15ee --- /dev/null +++ b/libs/libcodec2/src/ampexp.h @@ -0,0 +1,39 @@ +/*---------------------------------------------------------------------------*\ + + FILE........: ampexp.h + AUTHOR......: David Rowe + DATE CREATED: & August 2012 + + Functions for experimenting with amplitude quantisation. + +\*---------------------------------------------------------------------------*/ + +/* + Copyright (C) 2012 David Rowe + + All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 2.1, as + published by the Free Software Foundation. This program is + distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program; if not,see . +*/ + +#ifndef __AMPEX__ +#define __AMPEXP__ + +#include "defines.h" + +struct AEXP; + +struct AEXP *amp_experiment_create(); +void amp_experiment_destroy(struct AEXP *aexp); +void amp_experiment(struct AEXP *aexp, MODEL *model, char *arg); + +#endif diff --git a/libs/libcodec2/src/c2dec.c b/libs/libcodec2/src/c2dec.c index 3b876bcac0..fd4a04d31f 100644 --- a/libs/libcodec2/src/c2dec.c +++ b/libs/libcodec2/src/c2dec.c @@ -4,12 +4,7 @@ AUTHOR......: David Rowe DATE CREATED: 23/8/2010 - Decodes a file of bits to a file of raw speech samples using codec2. Demo - program for codec2. - - NOTE: the bit file is not packed, 51 bits/frame actually consumes 51 - bytes/frame on disk. If you are using this for a real world - application you may want to pack the 51 bytes into 7 bytes. + Decodes a file of bits to a file of raw speech samples using codec2. \*---------------------------------------------------------------------------*/ @@ -27,8 +22,7 @@ License for more details. You should have received a copy of the GNU Lesser General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program; if not, see . */ #include "codec2.h" @@ -38,41 +32,147 @@ #include #include +#define NONE 0 /* no bit errors */ +#define UNIFORM 1 /* random bit errors */ +#define TWO_STATE 2 /* Two state error model */ + int main(int argc, char *argv[]) { - static const int bitsSize = ((CODEC2_BITS_PER_FRAME + 7) / 8); - void *codec2; - FILE *fin; - FILE *fout; - short buf[CODEC2_SAMPLES_PER_FRAME]; - unsigned char bits[bitsSize]; - - if (argc != 3) { - printf("usage: %s InputBitFile OutputRawSpeechFile\n", argv[0]); + int mode; + void *codec2; + FILE *fin; + FILE *fout; + short *buf; + unsigned char *bits; + int nsam, nbit, nbyte, i, byte, frames, bit_errors, error_mode; + int state, next_state; + float ber, r, pstate0, pstate1; + + if (argc < 4) { + printf("basic usage...............: c2dec 3200|2400|1400|1200 InputBitFile OutputRawSpeechFile\n"); + printf("uniform errors usage.......: c2dec 3200|2400|1400|1200 InputBitFile OutputRawSpeechFile uniformBER\n"); + printf("two state fading usage....: c2dec 3200|2400|1400|1200 InputBitFile OutputRawSpeechFile probGood probBad\n"); + printf("e.g c2dec 1400 hts1a.c2 hts1a_1400.raw\n"); + printf("e.g c2dec 1400 hts1a.c2 hts1a_1400.raw 0.9\n"); + printf("e.g c2dec 1400 hts1a.c2 hts1a_1400.raw 0.99 0.9\n"); exit(1); } - - if ( (fin = fopen(argv[1],"rb")) == NULL ) { + + if (strcmp(argv[1],"3200") == 0) + mode = CODEC2_MODE_3200; + else if (strcmp(argv[1],"2400") == 0) + mode = CODEC2_MODE_2400; + else if (strcmp(argv[1],"1400") == 0) + mode = CODEC2_MODE_1400; + else if (strcmp(argv[1],"1200") == 0) + mode = CODEC2_MODE_1200; + else { + fprintf(stderr, "Error in mode: %s. Must be 4800, 3200, 2400, 1400 or 1200\n", argv[1]); + exit(1); + } + + if (strcmp(argv[2], "-") == 0) fin = stdin; + else if ( (fin = fopen(argv[2],"rb")) == NULL ) { fprintf(stderr, "Error opening input bit file: %s: %s.\n", - argv[1], strerror(errno)); + argv[2], strerror(errno)); exit(1); } - if ( (fout = fopen(argv[2],"wb")) == NULL ) { + if (strcmp(argv[3], "-") == 0) fout = stdout; + else if ( (fout = fopen(argv[3],"wb")) == NULL ) { fprintf(stderr, "Error opening output speech file: %s: %s.\n", - argv[2], strerror(errno)); + argv[3], strerror(errno)); exit(1); } - codec2 = codec2_create(); + error_mode = NONE; + ber = 0.0; + pstate0 = pstate1 = 0.0; + + if (argc == 5) { + error_mode = UNIFORM; + ber = atof(argv[4]); + } + + if (argc == 6) { + error_mode = TWO_STATE; + pstate0 = atof(argv[4]); + pstate1 = atof(argv[5]); + state = 0; + } + + codec2 = codec2_create(mode); + nsam = codec2_samples_per_frame(codec2); + nbit = codec2_bits_per_frame(codec2); + buf = (short*)malloc(nsam*sizeof(short)); + nbyte = (nbit + 7) / 8; + bits = (unsigned char*)malloc(nbyte*sizeof(char)); + frames = bit_errors = 0; + + while(fread(bits, sizeof(char), nbyte, fin) == (size_t)nbyte) { + frames++; + if (error_mode == UNIFORM) { + for(i=0; i pstate0) + next_state = 1; + break; + + case 1: + + /* burst error state - 50% bit error rate */ + + for(i=0; i pstate1) + next_state = 0; + break; + + } + + state = next_state; + } - while(fread(bits, sizeof(char), bitsSize, fin) == bitsSize) { codec2_decode(codec2, buf, bits); - fwrite(buf, sizeof(short), CODEC2_SAMPLES_PER_FRAME, fout); + fwrite(buf, sizeof(short), nsam, fout); + //if this is in a pipeline, we probably don't want the usual + //buffering to occur + if (fout == stdout) fflush(stdout); + if (fin == stdin) fflush(stdin); } + if (ber != 0.0) + fprintf(stderr, "actual BER: %1.3f\n", (float)bit_errors/(frames*nbit)); + codec2_destroy(codec2); + free(buf); + free(bits); fclose(fin); fclose(fout); diff --git a/libs/libcodec2/src/c2demo.c b/libs/libcodec2/src/c2demo.c new file mode 100644 index 0000000000..3b6741407b --- /dev/null +++ b/libs/libcodec2/src/c2demo.c @@ -0,0 +1,92 @@ +/*---------------------------------------------------------------------------*\ + + FILE........: c2demo.c + AUTHOR......: David Rowe + DATE CREATED: 15/11/2010 + + Encodes and decodes a file of raw speech samples using Codec 2. + Demonstrates use of Codec 2 function API. + + Note to convert a wave file to raw and vice-versa: + + $ sox file.wav -r 8000 -s -2 file.raw + $ sox -r 8000 -s -2 file.raw file.wav + +\*---------------------------------------------------------------------------*/ + +/* + Copyright (C) 2010 David Rowe + + All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 2.1, as + published by the Free Software Foundation. This program is + distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program; if not, see . +*/ + +#include "codec2.h" + +#include +#include +#include +#include + +#define BITS_SIZE ((CODEC2_BITS_PER_FRAME + 7) / 8) + +int main(int argc, char *argv[]) +{ + struct CODEC2 *codec2; + FILE *fin; + FILE *fout; + short *buf; + unsigned char *bits; + int nsam, nbit; + + if (argc != 3) { + printf("usage: %s InputRawSpeechFile OutputRawSpeechFile\n", argv[0]); + exit(1); + } + + if ( (fin = fopen(argv[1],"rb")) == NULL ) { + fprintf(stderr, "Error opening input speech file: %s: %s.\n", + argv[1], strerror(errno)); + exit(1); + } + + if ( (fout = fopen(argv[2],"wb")) == NULL ) { + fprintf(stderr, "Error opening output speech file: %s: %s.\n", + argv[2], strerror(errno)); + exit(1); + } + + /* Note only one set of Codec 2 states is required for an encoder + and decoder pair. */ + + codec2 = codec2_create(CODEC2_MODE_1400); + nsam = codec2_samples_per_frame(codec2); + buf = (short*)malloc(nsam*sizeof(short)); + nbit = codec2_bits_per_frame(codec2); + bits = (unsigned char*)malloc(nbit*sizeof(char)); + + while(fread(buf, sizeof(short), nsam, fin) == (size_t)nsam) { + codec2_encode(codec2, bits, buf); + codec2_decode(codec2, buf, bits); + fwrite(buf, sizeof(short), nsam, fout); + } + + free(buf); + free(bits); + codec2_destroy(codec2); + + fclose(fin); + fclose(fout); + + return 0; +} diff --git a/libs/libcodec2/src/c2enc.c b/libs/libcodec2/src/c2enc.c index 8fd7c7778d..d171c39a67 100644 --- a/libs/libcodec2/src/c2enc.c +++ b/libs/libcodec2/src/c2enc.c @@ -4,13 +4,8 @@ AUTHOR......: David Rowe DATE CREATED: 23/8/2010 - Encodes a file of raw speech samples using codec2 and ouputs a file - of bits (each bit is stored in the LSB or each output byte). Demo - program for codec2. - - NOTE: the bit file is not packed, 51 bits/frame actually consumes 51 - bytes/frame on disk. If you are using this for a real world - application you may want to pack the 51 bytes into 7 bytes. + Encodes a file of raw speech samples using codec2 and outputs a file + of bits. \*---------------------------------------------------------------------------*/ @@ -28,8 +23,7 @@ License for more details. You should have received a copy of the GNU Lesser General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program; if not, see . */ #include "codec2.h" @@ -41,40 +35,68 @@ int main(int argc, char *argv[]) { - static const int bitsSize = ((CODEC2_BITS_PER_FRAME + 7) / 8); - void *codec2; - FILE *fin; - FILE *fout; - short buf[CODEC2_SAMPLES_PER_FRAME]; - unsigned char bits[bitsSize]; - - if (argc != 3) { - printf("usage: %s InputRawspeechFile OutputBitFile\n", argv[0]); + int mode; + void *codec2; + FILE *fin; + FILE *fout; + short *buf; + unsigned char *bits; + int nsam, nbit, nbyte; + + if (argc != 4) { + printf("usage: c2enc 3200|2400|1400|1200 InputRawspeechFile OutputBitFile\n"); + printf("e.g c2enc 1400 ../raw/hts1a.raw hts1a.c2\n"); exit(1); } - if ( (fin = fopen(argv[1],"rb")) == NULL ) { - fprintf(stderr, "Error opening input bit file: %s: %s.\n", - argv[1], strerror(errno)); + if (strcmp(argv[1],"3200") == 0) + mode = CODEC2_MODE_3200; + else if (strcmp(argv[1],"2400") == 0) + mode = CODEC2_MODE_2400; + else if (strcmp(argv[1],"1400") == 0) + mode = CODEC2_MODE_1400; + else if (strcmp(argv[1],"1200") == 0) + mode = CODEC2_MODE_1200; + else { + fprintf(stderr, "Error in mode: %s. Must be 3200, 2400, 1400 or 1200\n", argv[1]); exit(1); } - if ( (fout = fopen(argv[2],"wb")) == NULL ) { - fprintf(stderr, "Error opening output speech file: %s: %s.\n", + if (strcmp(argv[2], "-") == 0) fin = stdin; + else if ( (fin = fopen(argv[2],"rb")) == NULL ) { + fprintf(stderr, "Error opening input speech file: %s: %s.\n", argv[2], strerror(errno)); exit(1); } - codec2 = codec2_create(); + if (strcmp(argv[3], "-") == 0) fout = stdout; + else if ( (fout = fopen(argv[3],"wb")) == NULL ) { + fprintf(stderr, "Error opening output compressed bit file: %s: %s.\n", + argv[3], strerror(errno)); + exit(1); + } + + codec2 = codec2_create(mode); + nsam = codec2_samples_per_frame(codec2); + nbit = codec2_bits_per_frame(codec2); + buf = (short*)malloc(nsam*sizeof(short)); + nbyte = (nbit + 7) / 8; - while(fread(buf, sizeof(short), CODEC2_SAMPLES_PER_FRAME, fin) == - CODEC2_SAMPLES_PER_FRAME) { + bits = (unsigned char*)malloc(nbyte*sizeof(char)); + + while(fread(buf, sizeof(short), nsam, fin) == (size_t)nsam) { codec2_encode(codec2, bits, buf); - fwrite(bits, sizeof(char), bitsSize, fout); + fwrite(bits, sizeof(char), nbyte, fout); + // if this is in a pipeline, we probably don't want the usual + // buffering to occur + if (fout == stdout) fflush(stdout); + if (fin == stdin) fflush(stdin); } codec2_destroy(codec2); + free(buf); + free(bits); fclose(fin); fclose(fout); diff --git a/libs/libcodec2/src/c2sim.c b/libs/libcodec2/src/c2sim.c index b9e5f0f78a..d47f0bbd42 100644 --- a/libs/libcodec2/src/c2sim.c +++ b/libs/libcodec2/src/c2sim.c @@ -4,8 +4,9 @@ AUTHOR......: David Rowe DATE CREATED: 20/8/2010 - Codec2 simulation. Combines encoder and decoder and allows switching in - out various algorithms and quantisation steps. + Codec2 simulation. Combines encoder and decoder and allows + switching in and out various algorithms and quantisation steps. Used + for algorithm development. \*---------------------------------------------------------------------------*/ @@ -23,8 +24,7 @@ License for more details. You should have received a copy of the GNU Lesser General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program; if not, see . */ #include @@ -33,6 +33,8 @@ #include #include #include +#include +#include #include "defines.h" #include "sine.h" @@ -44,32 +46,12 @@ #include "phase.h" #include "postfilter.h" #include "interp.h" +#include "ampexp.h" +#include "phaseexp.h" -/*---------------------------------------------------------------------------*\ - - switch_present() - - Searches the command line arguments for a "switch". If the switch is - found, returns the command line argument where it ws found, else returns - NULL. - -\*---------------------------------------------------------------------------*/ +void synth_one_frame(kiss_fft_cfg fft_inv_cfg, short buf[], MODEL *model, float Sn_[], float Pn[], int prede, float *de_mem, float gain); +void print_help(const struct option *long_options, int num_opts, char* argv[]); -int switch_present(sw,argc,argv) -register char sw[]; /* switch in string form */ -register int argc; /* number of command line arguments */ -register char *argv[]; /* array of command line arguments in string form */ -{ - register int i; /* loop variable */ - - for(i=1; i 20)) { - fprintf(stderr, "Error in lpc order: %d\n", order); - exit(1); - } - } - - dump = switch_present("--dump",argc,argv); - if (dump) - dump_on(argv[dump+1]); - - lsp = switch_present("--lsp",argc,argv); - lsp_quantiser = 0; - - phase0 = switch_present("--phase0",argc,argv); - if (phase0) { - ex_phase[0] = 0; - } - - hand_voicing = switch_present("--hand_voicing",argc,argv); - if (hand_voicing) { - fvoicing = fopen(argv[hand_voicing+1],"rt"); - assert(fvoicing != NULL); - } - - bg_est = 0.0; - postfilt = switch_present("--postfilter",argc,argv); - - decimate = switch_present("--dec",argc,argv); - - /* Initialise ------------------------------------------------------------*/ - - make_analysis_window(w,W); - make_synthesis_window(Pn); - quantise_init(); - - /* Main loop ------------------------------------------------------------*/ - - frames = 0; - sum_snr = 0; - while(fread(buf,sizeof(short),N,fin)) { - frames++; + FILE *fout = NULL; /* output speech file */ + FILE *fin; /* input speech file */ + short buf[N]; /* input/output buffer */ + float Sn[M]; /* float input speech samples */ + float Sn_pre[M]; /* pre-emphasised input speech samples */ + COMP Sw[FFT_ENC]; /* DFT of Sn[] */ + kiss_fft_cfg fft_fwd_cfg; + kiss_fft_cfg fft_inv_cfg; + float w[M]; /* time domain hamming window */ + COMP W[FFT_ENC]; /* DFT of w[] */ + MODEL model; + float Pn[2*N]; /* trapezoidal synthesis window */ + float Sn_[2*N]; /* synthesised speech */ + int i; /* loop variable */ + int frames; + float prev_Wo, prev__Wo, uq_Wo, prev_uq_Wo; + float pitch; + int voiced1 = 0; + char out_file[MAX_STR]; + char ampexp_arg[MAX_STR]; + char phaseexp_arg[MAX_STR]; + float snr; + float sum_snr; + + int lpc_model = 0, order = LPC_ORD; + int lsp = 0, lspd = 0, lspvq = 0; + int lspres = 0; + int lspdt = 0, lspdt_mode = LSPDT_ALL; + int dt = 0, lspjvm = 0, lspanssi = 0, lspjnd = 0, lspmel = 0; + int prede = 0; + float pre_mem = 0.0, de_mem = 0.0; + float ak[LPC_MAX]; + COMP Sw_[FFT_ENC]; + COMP Ew[FFT_ENC]; + + int phase0 = 0; + float ex_phase[MAX_AMP+1]; + + int postfilt; + float bg_est; + + int hand_voicing = 0, phaseexp = 0, ampexp = 0, hi = 0, simlpcpf = 0; + int lpcpf = 0; + FILE *fvoicing = 0; + + MODEL prev_model, interp_model; + int decimate = 0; + float lsps[LPC_MAX]; + float prev_lsps[LPC_MAX], prev_lsps_[LPC_MAX]; + float lsps__prev[LPC_MAX]; + float lsps__prev2[LPC_MAX]; + float e, prev_e; + float ak_interp[LPC_MAX]; + int lsp_indexes[LPC_MAX]; + float lsps_[LPC_MAX]; + float Woe_[2]; + + void *nlp_states; + float hpf_states[2]; + int scalar_quant_Wo_e = 0; + int vector_quant_Wo_e = 0; + int dump_pitch_e = 0; + FILE *fjvm = NULL; + #ifdef DUMP + int dump; + #endif + struct PEXP *pexp = NULL; + struct AEXP *aexp = NULL; + float gain = 1.0; + + char* opt_string = "ho:"; + struct option long_options[] = { + { "lpc", required_argument, &lpc_model, 1 }, + { "lspjnd", no_argument, &lspjnd, 1 }, + { "lspmel", no_argument, &lspmel, 1 }, + { "lsp", no_argument, &lsp, 1 }, + { "lspd", no_argument, &lspd, 1 }, + { "lspvq", no_argument, &lspvq, 1 }, + { "lspres", no_argument, &lspres, 1 }, + { "lspdt", no_argument, &lspdt, 1 }, + { "lspdt_mode", required_argument, NULL, 0 }, + { "lspjvm", no_argument, &lspjvm, 1 }, + { "lspanssi", no_argument, &lspanssi, 1 }, + { "phase0", no_argument, &phase0, 1 }, + { "phaseexp", required_argument, &phaseexp, 1 }, + { "ampexp", required_argument, &exp, 1 }, + { "postfilter", no_argument, &postfilt, 1 }, + { "hand_voicing", required_argument, &hand_voicing, 1 }, + { "dec", no_argument, &decimate, 1 }, + { "dt", no_argument, &dt, 1 }, + { "hi", no_argument, &hi, 1 }, + { "simlpcpf", no_argument, &simlpcpf, 1 }, + { "lpcpf", no_argument, &lpcpf, 1 }, + { "prede", no_argument, &prede, 1 }, + { "dump_pitch_e", required_argument, &dump_pitch_e, 1 }, + { "sq_pitch_e", no_argument, &scalar_quant_Wo_e, 1 }, + { "vq_pitch_e", no_argument, &vector_quant_Wo_e, 1 }, + { "rate", required_argument, NULL, 0 }, + { "gain", required_argument, NULL, 0 }, + #ifdef DUMP + { "dump", required_argument, &dump, 1 }, + #endif + { "help", no_argument, NULL, 'h' }, + { NULL, no_argument, NULL, 0 } + }; + int num_opts=sizeof(long_options)/sizeof(struct option); - /* Read input speech */ + for(i=0; i 20)) { + fprintf(stderr, "Error in LPC order: %s\n", optarg); + exit(1); + } + #ifdef DUMP + } else if(strcmp(long_options[option_index].name, "dump") == 0) { + if (dump) + dump_on(optarg); + #endif + } else if(strcmp(long_options[option_index].name, "lsp") == 0 + || strcmp(long_options[option_index].name, "lspd") == 0 + || strcmp(long_options[option_index].name, "lspvq") == 0) { + assert(order == LPC_ORD); + } else if(strcmp(long_options[option_index].name, "lspdt_mode") == 0) { + if (strcmp(optarg,"all") == 0) + lspdt_mode = LSPDT_ALL; + else if (strcmp(optarg,"low") == 0) + lspdt_mode = LSPDT_LOW; + else if (strcmp(optarg,"high") == 0) + lspdt_mode = LSPDT_HIGH; + else { + fprintf(stderr, "Error in lspdt_mode: %s\n", optarg); + exit(1); + } + } else if(strcmp(long_options[option_index].name, "hand_voicing") == 0) { + if ((fvoicing = fopen(optarg,"rt")) == NULL) { + fprintf(stderr, "Error opening voicing file: %s: %s.\n", + optarg, strerror(errno)); + exit(1); + } + } else if(strcmp(long_options[option_index].name, "dump_pitch_e") == 0) { + if ((fjvm = fopen(optarg,"wt")) == NULL) { + fprintf(stderr, "Error opening pitch & energy dump file: %s: %s.\n", + optarg, strerror(errno)); + exit(1); + } + } else if(strcmp(long_options[option_index].name, "phaseexp") == 0) { + strcpy(phaseexp_arg, optarg); + } else if(strcmp(long_options[option_index].name, "ampexp") == 0) { + strcpy(ampexp_arg, optarg); + } else if(strcmp(long_options[option_index].name, "gain") == 0) { + gain = atof(optarg); + } else if(strcmp(long_options[option_index].name, "rate") == 0) { + if(strcmp(optarg,"3200") == 0) { + lpc_model = 1; order = 10; + scalar_quant_Wo_e = 1; + lspd = 1; + phase0 = 1; + postfilt = 1; + decimate = 1; + lpcpf = 1; + } else if(strcmp(optarg,"2400") == 0) { + lpc_model = 1; order = 10; + vector_quant_Wo_e = 1; + lsp = 1; + phase0 = 1; + postfilt = 1; + decimate = 1; + lpcpf = 1; + } else if(strcmp(optarg,"1400") == 0) { + lpc_model = 1; order = 10; + vector_quant_Wo_e = 1; + lsp = 1; lspdt = 1; + phase0 = 1; + postfilt = 1; + decimate = 1; + dt = 1; + lpcpf = 1; + } else if(strcmp(optarg,"1200") == 0) { + lpc_model = 1; order = 10; + scalar_quant_Wo_e = 1; + lspjvm = 1; lspdt = 1; + phase0 = 1; + postfilt = 1; + decimate = 1; + dt = 1; + lpcpf = 1; + } else { + fprintf(stderr, "Error: invalid output rate %s\n", optarg); + exit(1); + } + } + break; + + case 'h': + print_help(long_options, num_opts, argv); + break; + + case 'o': + if (strcmp(optarg, "-") == 0) fout = stdout; + else if ((fout = fopen(optarg,"wb")) == NULL) { + fprintf(stderr, "Error opening output speech file: %s: %s.\n", + optarg, strerror(errno)); + exit(1); + } + strcpy(out_file,optarg); + break; + + default: + /* This will never be reached */ + break; + } + } - if (phase0) { - float Wn[M]; /* windowed speech samples */ - float Rk[LPC_ORD+1]; /* autocorrelation coeffs */ - - dump_phase(&model.phi[0], model.L); + /* Input file */ - /* find aks here, these are overwritten if LPC modelling is enabled */ + if ((fin = fopen(argv[optind],"rb")) == NULL) { + fprintf(stderr, "Error opening input speech file: %s: %s.\n", + argv[optind], strerror(errno)); + exit(1); + } - for(i=0; i 32767.0) buf[i] = 32767; else if (Sn_[i] < -32767.0) @@ -406,3 +884,36 @@ void synth_one_frame(short buf[], MODEL *model, float Sn_[], float Pn[]) } } + +void print_help(const struct option* long_options, int num_opts, char* argv[]) +{ + int i; + char *option_parameters; + + fprintf(stderr, "\nCodec2 - low bit rate speech codec - Simulation Program\n" + "\thttp://rowetel.com/codec2.html\n\n" + "usage: %s [OPTIONS] \n\n" + "Options:\n" + "\t-o \n", argv[0]); + for(i=0; i. */ #include @@ -43,19 +42,8 @@ #include "interp.h" #include "postfilter.h" #include "codec2.h" - -typedef struct { - float Sn[M]; /* input speech */ - float w[M]; /* time domain hamming window */ - COMP W[FFT_ENC]; /* DFT of w[] */ - float Pn[2*N]; /* trapezoidal synthesis window */ - float Sn_[2*N]; /* synthesised speech */ - float prev_Wo; /* previous frame's pitch estimate */ - float ex_phase; /* excitation model phase track */ - float bg_est; /* background noise estimate for post filter */ - MODEL prev_model; /* model parameters from 20ms ago */ - void *nlp; /* pitch predictor states */ -} CODEC2; +#include "lsp.h" +#include "codec2_internal.h" /*---------------------------------------------------------------------------*\ @@ -63,8 +51,18 @@ typedef struct { \*---------------------------------------------------------------------------*/ -void analyse_one_frame(CODEC2 *c2, MODEL *model, short speech[]); -void synthesise_one_frame(CODEC2 *c2, short speech[], MODEL *model,float ak[]); +void analyse_one_frame(struct CODEC2 *c2, MODEL *model, short speech[]); +void synthesise_one_frame(struct CODEC2 *c2, short speech[], MODEL *model, + float ak[]); +void codec2_encode_3200(struct CODEC2 *c2, unsigned char * bits, short speech[]); +void codec2_decode_3200(struct CODEC2 *c2, short speech[], const unsigned char * bits); +void codec2_encode_2400(struct CODEC2 *c2, unsigned char * bits, short speech[]); +void codec2_decode_2400(struct CODEC2 *c2, short speech[], const unsigned char * bits); +void codec2_encode_1400(struct CODEC2 *c2, unsigned char * bits, short speech[]); +void codec2_decode_1400(struct CODEC2 *c2, short speech[], const unsigned char * bits); +void codec2_encode_1200(struct CODEC2 *c2, unsigned char * bits, short speech[]); +void codec2_decode_1200(struct CODEC2 *c2, short speech[], const unsigned char * bits); +void ear_protection(float in_out[], int n); /*---------------------------------------------------------------------------*\ @@ -86,29 +84,46 @@ void synthesise_one_frame(CODEC2 *c2, short speech[], MODEL *model,float ak[]); \*---------------------------------------------------------------------------*/ -void *codec2_create() +struct CODEC2 * CODEC2_WIN32SUPPORT codec2_create(int mode) { - CODEC2 *c2; - int i,l; + struct CODEC2 *c2; + int i,l; - c2 = (CODEC2*)malloc(sizeof(CODEC2)); + c2 = (struct CODEC2*)malloc(sizeof(struct CODEC2)); if (c2 == NULL) return NULL; - + + assert( + (mode == CODEC2_MODE_3200) || + (mode == CODEC2_MODE_2400) || + (mode == CODEC2_MODE_1400) || + (mode == CODEC2_MODE_1200) + ); + c2->mode = mode; for(i=0; iSn[i] = 1.0; + c2->hpf_states[0] = c2->hpf_states[1] = 0.0; for(i=0; i<2*N; i++) c2->Sn_[i] = 0; - make_analysis_window(c2->w,c2->W); + c2->fft_fwd_cfg = kiss_fft_alloc(FFT_ENC, 0, NULL, NULL); + make_analysis_window(c2->fft_fwd_cfg, c2->w,c2->W); make_synthesis_window(c2->Pn); + c2->fft_inv_cfg = kiss_fft_alloc(FFT_DEC, 1, NULL, NULL); quantise_init(); - c2->prev_Wo = 0.0; + c2->prev_Wo_enc = 0.0; c2->bg_est = 0.0; c2->ex_phase = 0.0; for(l=1; l<=MAX_AMP; l++) - c2->prev_model.A[l] = 0.0; - c2->prev_model.Wo = TWO_PI/P_MAX; + c2->prev_model_dec.A[l] = 0.0; + c2->prev_model_dec.Wo = TWO_PI/P_MAX; + c2->prev_model_dec.L = PI/c2->prev_model_dec.Wo; + c2->prev_model_dec.voiced = 0; + + for(i=0; iprev_lsps_dec[i] = i*PI/(LPC_ORD+1); + } + c2->prev_e_dec = 1; c2->nlp = nlp_create(); if (c2->nlp == NULL) { @@ -116,12 +131,17 @@ void *codec2_create() return NULL; } - return (void*)c2; + c2->lpc_pf = 1; c2->bass_boost = 1; c2->beta = LPCPF_BETA; c2->gamma = LPCPF_GAMMA; + + c2->xq_enc[0] = c2->xq_enc[1] = 0.0; + c2->xq_dec[0] = c2->xq_dec[1] = 0.0; + + return c2; } /*---------------------------------------------------------------------------*\ - FUNCTION....: codec2_create + FUNCTION....: codec2_destroy AUTHOR......: David Rowe DATE CREATED: 21/8/2010 @@ -129,27 +149,263 @@ void *codec2_create() \*---------------------------------------------------------------------------*/ -void codec2_destroy(void *codec2_state) +void CODEC2_WIN32SUPPORT codec2_destroy(struct CODEC2 *c2) { - CODEC2 *c2; - - assert(codec2_state != NULL); - c2 = (CODEC2*)codec2_state; + assert(c2 != NULL); nlp_destroy(c2->nlp); - free(codec2_state); + KISS_FFT_FREE(c2->fft_fwd_cfg); + KISS_FFT_FREE(c2->fft_inv_cfg); + free(c2); +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: codec2_bits_per_frame + AUTHOR......: David Rowe + DATE CREATED: Nov 14 2011 + + Returns the number of bits per frame. + +\*---------------------------------------------------------------------------*/ + +int CODEC2_WIN32SUPPORT codec2_bits_per_frame(struct CODEC2 *c2) { + if (c2->mode == CODEC2_MODE_3200) + return 64; + if (c2->mode == CODEC2_MODE_2400) + return 48; + if (c2->mode == CODEC2_MODE_1400) + return 56; + if (c2->mode == CODEC2_MODE_1200) + return 48; + + return 0; /* shouldn't get here */ +} + + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: codec2_samples_per_frame + AUTHOR......: David Rowe + DATE CREATED: Nov 14 2011 + + Returns the number of bits per frame. + +\*---------------------------------------------------------------------------*/ + +int CODEC2_WIN32SUPPORT codec2_samples_per_frame(struct CODEC2 *c2) { + if (c2->mode == CODEC2_MODE_3200) + return 160; + if (c2->mode == CODEC2_MODE_2400) + return 160; + if (c2->mode == CODEC2_MODE_1400) + return 320; + if (c2->mode == CODEC2_MODE_1200) + return 320; + + return 0; /* shouldnt get here */ +} + +void CODEC2_WIN32SUPPORT codec2_encode(struct CODEC2 *c2, unsigned char *bits, short speech[]) +{ + assert(c2 != NULL); + assert( + (c2->mode == CODEC2_MODE_3200) || + (c2->mode == CODEC2_MODE_2400) || + (c2->mode == CODEC2_MODE_1400) || + (c2->mode == CODEC2_MODE_1200) + ); + + if (c2->mode == CODEC2_MODE_3200) + codec2_encode_3200(c2, bits, speech); + if (c2->mode == CODEC2_MODE_2400) + codec2_encode_2400(c2, bits, speech); + if (c2->mode == CODEC2_MODE_1400) + codec2_encode_1400(c2, bits, speech); + if (c2->mode == CODEC2_MODE_1200) + codec2_encode_1200(c2, bits, speech); +} + +void CODEC2_WIN32SUPPORT codec2_decode(struct CODEC2 *c2, short speech[], const unsigned char *bits) +{ + assert(c2 != NULL); + assert( + (c2->mode == CODEC2_MODE_3200) || + (c2->mode == CODEC2_MODE_2400) || + (c2->mode == CODEC2_MODE_1400) || + (c2->mode == CODEC2_MODE_1200) + ); + + if (c2->mode == CODEC2_MODE_3200) + codec2_decode_3200(c2, speech, bits); + if (c2->mode == CODEC2_MODE_2400) + codec2_decode_2400(c2, speech, bits); + if (c2->mode == CODEC2_MODE_1400) + codec2_decode_1400(c2, speech, bits); + if (c2->mode == CODEC2_MODE_1200) + codec2_decode_1200(c2, speech, bits); +} + + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: codec2_encode_3200 + AUTHOR......: David Rowe + DATE CREATED: 13 Sep 2012 + + Encodes 160 speech samples (20ms of speech) into 64 bits. + + The codec2 algorithm actually operates internally on 10ms (80 + sample) frames, so we run the encoding algorithm twice. On the + first frame we just send the voicing bits. On the second frame we + send all model parameters. Compared to 2400 we use a larger number + of bits for the LSPs and non-VQ pitch and energy. + + The bit allocation is: + + Parameter bits/frame + -------------------------------------- + Harmonic magnitudes (LSPs) 50 + Pitch (Wo) 7 + Energy 5 + Voicing (10ms update) 2 + TOTAL 64 + +\*---------------------------------------------------------------------------*/ + +void codec2_encode_3200(struct CODEC2 *c2, unsigned char * bits, short speech[]) +{ + MODEL model; + float ak[LPC_ORD+1]; + float lsps[LPC_ORD]; + float e; + int Wo_index, e_index; + int lspd_indexes[LPC_ORD]; + int i; + unsigned int nbit = 0; + + assert(c2 != NULL); + + memset(bits, '\0', ((codec2_bits_per_frame(c2) + 7) / 8)); + + /* first 10ms analysis frame - we just want voicing */ + + analyse_one_frame(c2, &model, speech); + pack(bits, &nbit, model.voiced, 1); + + /* second 10ms analysis frame */ + + analyse_one_frame(c2, &model, &speech[N]); + pack(bits, &nbit, model.voiced, 1); + Wo_index = encode_Wo(model.Wo); + pack(bits, &nbit, Wo_index, WO_BITS); + + e = speech_to_uq_lsps(lsps, ak, c2->Sn, c2->w, LPC_ORD); + e_index = encode_energy(e); + pack(bits, &nbit, e_index, E_BITS); + + encode_lspds_scalar(lspd_indexes, lsps, LPC_ORD); + for(i=0; iprev_model_dec, &model[1]); + e[0] = interp_energy(c2->prev_e_dec, e[1]); + + /* LSPs are sampled every 20ms so we interpolate the frame in + between, then recover spectral amplitudes */ + + interpolate_lsp_ver2(&lsps[0][0], c2->prev_lsps_dec, &lsps[1][0], 0.5); + for(i=0; i<2; i++) { + lsp_to_lpc(&lsps[i][0], &ak[i][0], LPC_ORD); + aks_to_M2(c2->fft_fwd_cfg, &ak[i][0], LPC_ORD, &model[i], e[i], &snr, 0, 0, + c2->lpc_pf, c2->bass_boost, c2->beta, c2->gamma); + apply_lpc_correction(&model[i]); + } + + /* synthesise ------------------------------------------------*/ + + for(i=0; i<2; i++) + synthesise_one_frame(c2, &speech[N*i], &model[i], &ak[i][0]); + + /* update memories for next frame ----------------------------*/ + + c2->prev_model_dec = model[1]; + c2->prev_e_dec = e[1]; + for(i=0; iprev_lsps_dec[i] = lsps[1][i]; } + /*---------------------------------------------------------------------------*\ - FUNCTION....: codec2_encode + FUNCTION....: codec2_encode_2400 AUTHOR......: David Rowe DATE CREATED: 21/8/2010 - Encodes 160 speech samples (20ms of speech) into 51 bits. + Encodes 160 speech samples (20ms of speech) into 48 bits. The codec2 algorithm actually operates internally on 10ms (80 sample) frames, so we run the encoding algorithm twice. On the - first frame we just send the voicing bit. One the second frame we + first frame we just send the voicing bit. On the second frame we send all model parameters. The bit allocation is: @@ -157,115 +413,485 @@ void codec2_destroy(void *codec2_state) Parameter bits/frame -------------------------------------- Harmonic magnitudes (LSPs) 36 - Low frequency LPC correction 1 - Energy 5 - Wo (fundamental frequnecy) 7 + Joint VQ of Energy and Wo 8 Voicing (10ms update) 2 - TOTAL 51 + Spare 2 + TOTAL 48 \*---------------------------------------------------------------------------*/ -void codec2_encode(void *codec2_state, unsigned char * bits, short speech[]) +void codec2_encode_2400(struct CODEC2 *c2, unsigned char * bits, short speech[]) { - CODEC2 *c2; MODEL model; - int voiced1, voiced2; + float ak[LPC_ORD+1]; + float lsps[LPC_ORD]; + float e; + int WoE_index; int lsp_indexes[LPC_ORD]; - int lpc_correction; - int energy_index; - int Wo_index; int i; + int spare = 0; unsigned int nbit = 0; - assert(codec2_state != NULL); - c2 = (CODEC2*)codec2_state; + assert(c2 != NULL); + + memset(bits, '\0', ((codec2_bits_per_frame(c2) + 7) / 8)); /* first 10ms analysis frame - we just want voicing */ analyse_one_frame(c2, &model, speech); - voiced1 = model.voiced; + pack(bits, &nbit, model.voiced, 1); /* second 10ms analysis frame */ analyse_one_frame(c2, &model, &speech[N]); - voiced2 = model.voiced; + pack(bits, &nbit, model.voiced, 1); - Wo_index = encode_Wo(model.Wo); - encode_amplitudes(lsp_indexes, - &lpc_correction, - &energy_index, - &model, - c2->Sn, - c2->w); - memset(bits, '\0', ((CODEC2_BITS_PER_FRAME + 7) / 8)); - pack(bits, &nbit, Wo_index, WO_BITS); - for(i=0; iSn, c2->w, LPC_ORD); + WoE_index = encode_WoE(&model, e, c2->xq_enc); + pack(bits, &nbit, WoE_index, WO_E_BITS); + + encode_lsps_scalar(lsp_indexes, lsps, LPC_ORD); + for(i=0; ixq_dec, WoE_index); + + for(i=0; iprev_model_dec, &model[1]); + e[0] = interp_energy(c2->prev_e_dec, e[1]); + + /* LSPs are sampled every 20ms so we interpolate the frame in + between, then recover spectral amplitudes */ + + interpolate_lsp_ver2(&lsps[0][0], c2->prev_lsps_dec, &lsps[1][0], 0.5); + for(i=0; i<2; i++) { + lsp_to_lpc(&lsps[i][0], &ak[i][0], LPC_ORD); + aks_to_M2(c2->fft_fwd_cfg, &ak[i][0], LPC_ORD, &model[i], e[i], &snr, 0, 0, + c2->lpc_pf, c2->bass_boost, c2->beta, c2->gamma); + apply_lpc_correction(&model[i]); + } + + /* synthesise ------------------------------------------------*/ + + for(i=0; i<2; i++) + synthesise_one_frame(c2, &speech[N*i], &model[i], &ak[i][0]); + + /* update memories for next frame ----------------------------*/ + + c2->prev_model_dec = model[1]; + c2->prev_e_dec = e[1]; + for(i=0; iprev_lsps_dec[i] = lsps[1][i]; +} + + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: codec2_encode_1400 + AUTHOR......: David Rowe + DATE CREATED: May 11 2012 + + Encodes 320 speech samples (40ms of speech) into 56 bits. + + The codec2 algorithm actually operates internally on 10ms (80 + sample) frames, so we run the encoding algorithm 4 times: + + frame 0: voicing bit + frame 1: voicing bit, joint VQ of Wo and E + frame 2: voicing bit + frame 3: voicing bit, joint VQ of Wo and E, scalar LSPs + + The bit allocation is: + + Parameter frame 2 frame 4 Total + ------------------------------------------------------- + Harmonic magnitudes (LSPs) 0 36 36 + Energy+Wo 8 8 16 + Voicing (10ms update) 2 2 4 + TOTAL 10 46 56 + +\*---------------------------------------------------------------------------*/ + +void codec2_encode_1400(struct CODEC2 *c2, unsigned char * bits, short speech[]) +{ + MODEL model; + float lsps[LPC_ORD]; float ak[LPC_ORD+1]; + float e; + int lsp_indexes[LPC_ORD]; + int WoE_index; int i; unsigned int nbit = 0; - MODEL model_interp; - assert(codec2_state != NULL); - c2 = (CODEC2*)codec2_state; + assert(c2 != NULL); - Wo_index = unpack(bits, &nbit, WO_BITS); - for(i=0; iSn, c2->w, LPC_ORD); + + WoE_index = encode_WoE(&model, e, c2->xq_enc); + pack(bits, &nbit, WoE_index, WO_E_BITS); + + /* frame 3: - voicing ---------------------------------------------*/ + + analyse_one_frame(c2, &model, &speech[2*N]); + pack(bits, &nbit, model.voiced, 1); + + /* frame 4: - voicing, joint Wo & E, scalar LSPs ------------------*/ + + analyse_one_frame(c2, &model, &speech[3*N]); + pack(bits, &nbit, model.voiced, 1); + + e = speech_to_uq_lsps(lsps, ak, c2->Sn, c2->w, LPC_ORD); + WoE_index = encode_WoE(&model, e, c2->xq_enc); + pack(bits, &nbit, WoE_index, WO_E_BITS); + + encode_lsps_scalar(lsp_indexes, lsps, LPC_ORD); + for(i=0; ixq_dec, WoE_index); + + model[2].voiced = unpack(bits, &nbit, 1); + + model[3].voiced = unpack(bits, &nbit, 1); + WoE_index = unpack(bits, &nbit, WO_E_BITS); + decode_WoE(&model[3], &e[3], c2->xq_dec, WoE_index); + + for(i=0; iprev_model, &model); - - synthesise_one_frame(c2, speech, &model_interp, ak); - synthesise_one_frame(c2, &speech[N], &model, ak); - - memcpy(&c2->prev_model, &model, sizeof(MODEL)); + decode_lsps_scalar(&lsps[3][0], lsp_indexes, LPC_ORD); + check_lsp_order(&lsps[3][0], LPC_ORD); + bw_expand_lsps(&lsps[3][0], LPC_ORD); + + /* interpolate ------------------------------------------------*/ + + /* Wo and energy are sampled every 20ms, so we interpolate just 1 + 10ms frame between 20ms samples */ + + interp_Wo(&model[0], &c2->prev_model_dec, &model[1]); + e[0] = interp_energy(c2->prev_e_dec, e[1]); + interp_Wo(&model[2], &model[1], &model[3]); + e[2] = interp_energy(e[1], e[3]); + + /* LSPs are sampled every 40ms so we interpolate the 3 frames in + between, then recover spectral amplitudes */ + + for(i=0, weight=0.25; i<3; i++, weight += 0.25) { + interpolate_lsp_ver2(&lsps[i][0], c2->prev_lsps_dec, &lsps[3][0], weight); + } + for(i=0; i<4; i++) { + lsp_to_lpc(&lsps[i][0], &ak[i][0], LPC_ORD); + aks_to_M2(c2->fft_fwd_cfg, &ak[i][0], LPC_ORD, &model[i], e[i], &snr, 0, 0, + c2->lpc_pf, c2->bass_boost, c2->beta, c2->gamma); + apply_lpc_correction(&model[i]); + } + + /* synthesise ------------------------------------------------*/ + + for(i=0; i<4; i++) + synthesise_one_frame(c2, &speech[N*i], &model[i], &ak[i][0]); + + /* update memories for next frame ----------------------------*/ + + c2->prev_model_dec = model[3]; + c2->prev_e_dec = e[3]; + for(i=0; iprev_lsps_dec[i] = lsps[3][i]; + +} + + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: codec2_encode_1200 + AUTHOR......: David Rowe + DATE CREATED: Nov 14 2011 + + Encodes 320 speech samples (40ms of speech) into 48 bits. + + The codec2 algorithm actually operates internally on 10ms (80 + sample) frames, so we run the encoding algorithm four times: + + frame 0: voicing bit + frame 1: voicing bit, joint VQ of Wo and E + frame 2: voicing bit + frame 3: voicing bit, joint VQ of Wo and E, VQ LSPs + + The bit allocation is: + + Parameter frame 2 frame 4 Total + ------------------------------------------------------- + Harmonic magnitudes (LSPs) 0 27 27 + Energy+Wo 8 8 16 + Voicing (10ms update) 2 2 4 + Spare 0 1 1 + TOTAL 10 38 48 + +\*---------------------------------------------------------------------------*/ + +void codec2_encode_1200(struct CODEC2 *c2, unsigned char * bits, short speech[]) +{ + MODEL model; + float lsps[LPC_ORD]; + float lsps_[LPC_ORD]; + float ak[LPC_ORD+1]; + float e; + int lsp_indexes[LPC_ORD]; + int WoE_index; + int i; + int spare = 0; + unsigned int nbit = 0; + + assert(c2 != NULL); + + memset(bits, '\0', ((codec2_bits_per_frame(c2) + 7) / 8)); + + /* frame 1: - voicing ---------------------------------------------*/ + + analyse_one_frame(c2, &model, speech); + pack(bits, &nbit, model.voiced, 1); + + /* frame 2: - voicing, joint Wo & E -------------------------------*/ + + analyse_one_frame(c2, &model, &speech[N]); + pack(bits, &nbit, model.voiced, 1); + + /* need to run this just to get LPC energy */ + e = speech_to_uq_lsps(lsps, ak, c2->Sn, c2->w, LPC_ORD); + + WoE_index = encode_WoE(&model, e, c2->xq_enc); + pack(bits, &nbit, WoE_index, WO_E_BITS); + + /* frame 3: - voicing ---------------------------------------------*/ + + analyse_one_frame(c2, &model, &speech[2*N]); + pack(bits, &nbit, model.voiced, 1); + + /* frame 4: - voicing, joint Wo & E, scalar LSPs ------------------*/ + + analyse_one_frame(c2, &model, &speech[3*N]); + pack(bits, &nbit, model.voiced, 1); + + e = speech_to_uq_lsps(lsps, ak, c2->Sn, c2->w, LPC_ORD); + WoE_index = encode_WoE(&model, e, c2->xq_enc); + pack(bits, &nbit, WoE_index, WO_E_BITS); + + encode_lsps_vq(lsp_indexes, lsps, lsps_, LPC_ORD); + for(i=0; ixq_dec, WoE_index); + + model[2].voiced = unpack(bits, &nbit, 1); + + model[3].voiced = unpack(bits, &nbit, 1); + WoE_index = unpack(bits, &nbit, WO_E_BITS); + decode_WoE(&model[3], &e[3], c2->xq_dec, WoE_index); + + for(i=0; iprev_model_dec, &model[1]); + e[0] = interp_energy(c2->prev_e_dec, e[1]); + interp_Wo(&model[2], &model[1], &model[3]); + e[2] = interp_energy(e[1], e[3]); + + /* LSPs are sampled every 40ms so we interpolate the 3 frames in + between, then recover spectral amplitudes */ + + for(i=0, weight=0.25; i<3; i++, weight += 0.25) { + interpolate_lsp_ver2(&lsps[i][0], c2->prev_lsps_dec, &lsps[3][0], weight); + } + for(i=0; i<4; i++) { + lsp_to_lpc(&lsps[i][0], &ak[i][0], LPC_ORD); + aks_to_M2(c2->fft_fwd_cfg, &ak[i][0], LPC_ORD, &model[i], e[i], &snr, 0, 0, + c2->lpc_pf, c2->bass_boost, c2->beta, c2->gamma); + apply_lpc_correction(&model[i]); + } + + /* synthesise ------------------------------------------------*/ + + for(i=0; i<4; i++) + synthesise_one_frame(c2, &speech[N*i], &model[i], &ak[i][0]); + + /* update memories for next frame ----------------------------*/ + + c2->prev_model_dec = model[3]; + c2->prev_e_dec = e[3]; + for(i=0; iprev_lsps_dec[i] = lsps[3][i]; +} + + /*---------------------------------------------------------------------------*\ FUNCTION....: synthesise_one_frame() @@ -276,13 +902,14 @@ void codec2_decode(void *codec2_state, short speech[], \*---------------------------------------------------------------------------*/ -void synthesise_one_frame(CODEC2 *c2, short speech[], MODEL *model, float ak[]) +void synthesise_one_frame(struct CODEC2 *c2, short speech[], MODEL *model, float ak[]) { int i; - phase_synth_zero_order(model, ak, &c2->ex_phase); + phase_synth_zero_order(c2->fft_fwd_cfg, model, ak, &c2->ex_phase, LPC_ORD); postfilter(model, &c2->bg_est); - synthesise(c2->Sn_, model, c2->Pn, 1); + synthesise(c2->fft_inv_cfg, c2->Sn_, model, c2->Pn, 1); + ear_protection(c2->Sn_, N); for(i=0; iSn_[i] > 32767.0) @@ -306,11 +933,12 @@ void synthesise_one_frame(CODEC2 *c2, short speech[], MODEL *model, float ak[]) \*---------------------------------------------------------------------------*/ -void analyse_one_frame(CODEC2 *c2, MODEL *model, short speech[]) +void analyse_one_frame(struct CODEC2 *c2, MODEL *model, short speech[]) { COMP Sw[FFT_ENC]; COMP Sw_[FFT_ENC]; - float pitch; + COMP Ew[FFT_ENC]; + float pitch, snr; int i; /* Read input speech */ @@ -319,19 +947,113 @@ void analyse_one_frame(CODEC2 *c2, MODEL *model, short speech[]) c2->Sn[i] = c2->Sn[i+N]; for(i=0; iSn[i+M-N] = speech[i]; - dft_speech(Sw, c2->Sn, c2->w); + + dft_speech(c2->fft_fwd_cfg, Sw, c2->Sn, c2->w); /* Estimate pitch */ - nlp(c2->nlp,c2->Sn,N,M,P_MIN,P_MAX,&pitch,Sw,&c2->prev_Wo); - c2->prev_Wo = TWO_PI/pitch; + nlp(c2->nlp,c2->Sn,N,M,P_MIN,P_MAX,&pitch,Sw, c2->W, &c2->prev_Wo_enc); model->Wo = TWO_PI/pitch; model->L = PI/model->Wo; /* estimate model parameters */ - dft_speech(Sw, c2->Sn, c2->w); two_stage_pitch_refinement(model, Sw); estimate_amplitudes(model, Sw, c2->W); - est_voicing_mbe(model, Sw, c2->W, (FS/TWO_PI)*model->Wo, Sw_); + snr = est_voicing_mbe(model, Sw, c2->W, Sw_, Ew, c2->prev_Wo_enc); + //fprintf(stderr,"snr %3.2f v: %d Wo: %f prev_Wo: %f\n", + // snr, model->voiced, model->Wo, c2->prev_Wo_enc); + c2->prev_Wo_enc = model->Wo; } + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: ear_protection() + AUTHOR......: David Rowe + DATE CREATED: Nov 7 2012 + + Limits output level to protect ears when there are bit errors or the input + is overdriven. This doesn't correct or mask bit erros, just reduces the + worst of their damage. + +\*---------------------------------------------------------------------------*/ + +void ear_protection(float in_out[], int n) { + float max_sample, over, gain; + int i; + + /* find maximum sample in frame */ + + max_sample = 0.0; + for(i=0; i max_sample) + max_sample = in_out[i]; + + /* determine how far above set point */ + + over = max_sample/30000.0; + + /* If we are x dB over set point we reduce level by 2x dB, this + attenuates major excursions in amplitude (likely to be caused + by bit errors) more than smaller ones */ + + if (over > 1.0) { + gain = 1.0/(over*over); + //fprintf(stderr, "gain: %f\n", gain); + for(i=0; i= 0.0) && (beta <= 1.0)); + assert((gamma >= 0.0) && (gamma <= 1.0)); + c2->lpc_pf = enable; + c2->bass_boost = bass_boost; + c2->beta = beta; + c2->gamma = gamma; +} + +/* + Allows optional stealing of one of the voicing bits for use as a + spare bit, only 1400 bit/s supported for now. Experimental method + of sending voice/data frames for FreeDV. +*/ + +int CODEC2_WIN32SUPPORT codec2_get_spare_bit_index(struct CODEC2 *c2) +{ + assert(c2 != NULL); + + if (c2->mode != CODEC2_MODE_1400) + return -1; + + return 10; // bit 10 (11th bit) is v2 (third voicing bit) +} + +/* + Reconstructs the spare voicing bit. Note works on unpacked bits + for convenience. +*/ + +int CODEC2_WIN32SUPPORT codec2_rebuild_spare_bit(struct CODEC2 *c2, int unpacked_bits[]) +{ + int v0,v1,v3; + + assert(c2 != NULL); + + if (c2->mode != CODEC2_MODE_1400) + return -1; + + v0 = unpacked_bits[0]; + v1 = unpacked_bits[1]; + v3 = unpacked_bits[11]; + + /* if either adjacent frame is voiced, make this one voiced */ + + unpacked_bits[10] = (v1 || v3); + + return 0; +} + + diff --git a/libs/libcodec2/src/codec2.h b/libs/libcodec2/src/codec2.h index 7a1d1450a5..8baa307eb6 100644 --- a/libs/libcodec2/src/codec2.h +++ b/libs/libcodec2/src/codec2.h @@ -2,10 +2,10 @@ FILE........: codec2.h AUTHOR......: David Rowe - DATE CREATED: 21/8/2010 + DATE CREATED: 21 August 2010 - Codec2 fully quantised encoder and decoder functions. If you want use - codec2, these are the functions you need to call. + Codec 2 fully quantised encoder and decoder functions. If you want use + Codec 2, these are the functions you need to call. \*---------------------------------------------------------------------------*/ @@ -23,21 +23,50 @@ License for more details. You should have received a copy of the GNU Lesser General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program; if not, see . */ +#ifdef __cplusplus + extern "C" { +#endif + #ifndef __CODEC2__ #define __CODEC2__ -#include "codebook.h" -#define CODEC2_SAMPLES_PER_FRAME 160 -#define CODEC2_BITS_PER_FRAME 51 +/* set up the calling convention for DLL function import/export for + WIN32 cross compiling */ + +#ifdef __CODEC2_WIN32__ +#ifdef __CODEC2_BUILDING_DLL__ +#define CODEC2_WIN32SUPPORT __declspec(dllexport) __stdcall +#else +#define CODEC2_WIN32SUPPORT __declspec(dllimport) __stdcall +#endif +#else +#define CODEC2_WIN32SUPPORT +#endif + +#define CODEC2_MODE_3200 0 +#define CODEC2_MODE_2400 1 +#define CODEC2_MODE_1400 2 +#define CODEC2_MODE_1200 3 -void *codec2_create(); -void codec2_destroy(void *codec2_state); -void codec2_encode(void *codec2_state, unsigned char * bits, short speech_in[]); -void codec2_decode(void *codec2_state, short speech_out[], - const unsigned char * bits); +struct CODEC2; + +struct CODEC2 * CODEC2_WIN32SUPPORT codec2_create(int mode); +void CODEC2_WIN32SUPPORT codec2_destroy(struct CODEC2 *codec2_state); +void CODEC2_WIN32SUPPORT codec2_encode(struct CODEC2 *codec2_state, unsigned char * bits, short speech_in[]); +void CODEC2_WIN32SUPPORT codec2_decode(struct CODEC2 *codec2_state, short speech_out[], const unsigned char *bits); +int CODEC2_WIN32SUPPORT codec2_samples_per_frame(struct CODEC2 *codec2_state); +int CODEC2_WIN32SUPPORT codec2_bits_per_frame(struct CODEC2 *codec2_state); + +void CODEC2_WIN32SUPPORT codec2_set_lpc_post_filter(struct CODEC2 *codec2_state, int enable, int bass_boost, float beta, float gamma); +int CODEC2_WIN32SUPPORT codec2_get_spare_bit_index(struct CODEC2 *codec2_state); +int CODEC2_WIN32SUPPORT codec2_rebuild_spare_bit(struct CODEC2 *codec2_state, int unpacked_bits[]); #endif + +#ifdef __cplusplus +} +#endif + diff --git a/libs/libcodec2/src/codec2_internal.h b/libs/libcodec2/src/codec2_internal.h new file mode 100644 index 0000000000..5c6d279c80 --- /dev/null +++ b/libs/libcodec2/src/codec2_internal.h @@ -0,0 +1,60 @@ +/*---------------------------------------------------------------------------*\ + + FILE........: codec2_internal.h + AUTHOR......: David Rowe + DATE CREATED: April 16 2012 + + Header file for Codec2 internal states, exposed via this header + file to assist in testing. + +\*---------------------------------------------------------------------------*/ + +/* + Copyright (C) 2012 David Rowe + + All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 2.1, as + published by the Free Software Foundation. This program is + distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program; if not, see . +*/ + +#ifndef __CODEC2_INTERNAL__ +#define __CODEC2_INTERNAL__ + +struct CODEC2 { + int mode; + kiss_fft_cfg fft_fwd_cfg; /* forward FFT config */ + float w[M]; /* time domain hamming window */ + COMP W[FFT_ENC]; /* DFT of w[] */ + float Pn[2*N]; /* trapezoidal synthesis window */ + float Sn[M]; /* input speech */ + float hpf_states[2]; /* high pass filter states */ + void *nlp; /* pitch predictor states */ + + kiss_fft_cfg fft_inv_cfg; /* inverse FFT config */ + float Sn_[2*N]; /* synthesised output speech */ + float ex_phase; /* excitation model phase track */ + float bg_est; /* background noise estimate for post filter */ + float prev_Wo_enc; /* previous frame's pitch estimate */ + MODEL prev_model_dec; /* previous frame's model parameters */ + float prev_lsps_dec[LPC_ORD]; /* previous frame's LSPs */ + float prev_e_dec; /* previous frame's LPC energy */ + + int lpc_pf; /* LPC post filter on */ + int bass_boost; /* LPC post filter bass boost */ + float beta; /* LPC post filter parameters */ + float gamma; + + float xq_enc[2]; /* joint pitch and energy VQ states */ + float xq_dec[2]; +}; + +#endif diff --git a/libs/libcodec2/src/comp.h b/libs/libcodec2/src/comp.h index bca01b5d2f..cedcab37f2 100644 --- a/libs/libcodec2/src/comp.h +++ b/libs/libcodec2/src/comp.h @@ -22,8 +22,7 @@ License for more details. You should have received a copy of the GNU Lesser General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program; if not, see . */ #ifndef __COMP__ diff --git a/libs/libcodec2/src/defines.h b/libs/libcodec2/src/defines.h index ef4899f70a..4870770c98 100644 --- a/libs/libcodec2/src/defines.h +++ b/libs/libcodec2/src/defines.h @@ -1,7 +1,7 @@ /*---------------------------------------------------------------------------*\ FILE........: defines.h - AUTHOR......: David Rowe + AUTHOR......: David Rowe DATE CREATED: 23/4/93 Defines and structures used throughout the codec. @@ -22,8 +22,7 @@ License for more details. You should have received a copy of the GNU Lesser General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program; if not, see . */ #ifndef __DEFINES__ @@ -48,7 +47,7 @@ #define FFT_ENC 512 /* size of FFT used for encoder */ #define FFT_DEC 512 /* size of FFT used in decoder */ #define TW 40 /* Trapezoidal synthesis window overlap */ -#define V_THRESH 4.0 /* voicing threshold in dB */ +#define V_THRESH 6.0 /* voicing threshold in dB */ #define LPC_MAX 20 /* maximum LPC order */ #define LPC_ORD 10 /* phase modelling LPC order */ @@ -64,21 +63,32 @@ \*---------------------------------------------------------------------------*/ -/* Complex number */ - -typedef struct { - float real; - float imag; -} COMP; - /* Structure to hold model parameters for one frame */ typedef struct { float Wo; /* fundamental frequency estimate in radians */ int L; /* number of harmonics */ - float A[MAX_AMP]; /* amplitiude of each harmonic */ - float phi[MAX_AMP]; /* phase of each harmonic */ + float A[MAX_AMP+1]; /* amplitiude of each harmonic */ + float phi[MAX_AMP+1]; /* phase of each harmonic */ int voiced; /* non-zero if this frame is voiced */ } MODEL; +/* describes each codebook */ + +struct lsp_codebook { + int k; /* dimension of vector */ + int log2m; /* number of bits in m */ + int m; /* elements in codebook */ + const float * cb; /* The elements */ +}; + +extern const struct lsp_codebook lsp_cb[]; +extern const struct lsp_codebook lsp_cbd[]; +extern const struct lsp_codebook lsp_cbvq[]; +extern const struct lsp_codebook lsp_cbjnd[]; +extern const struct lsp_codebook lsp_cbdt[]; +extern const struct lsp_codebook lsp_cbjvm[]; +extern const struct lsp_codebook lsp_cbvqanssi[]; +extern const struct lsp_codebook ge_cb[]; + #endif diff --git a/libs/libcodec2/src/dump.c b/libs/libcodec2/src/dump.c index 2d18744483..b414c794d5 100644 --- a/libs/libcodec2/src/dump.c +++ b/libs/libcodec2/src/dump.c @@ -1,7 +1,7 @@ /*---------------------------------------------------------------------------*\ FILE........: dump.c - AUTHOR......: David Rowe + AUTHOR......: David Rowe DATE CREATED: 25/8/09 Routines to dump data to text files for Octave analysis. @@ -20,11 +20,11 @@ License for more details. You should have received a copy of the GNU Lesser General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program; if not, see . */ #include "defines.h" +#include "comp.h" #include "dump.h" #include #include @@ -32,15 +32,21 @@ #include #include +#ifdef DUMP static int dumpon = 0; static FILE *fsn = NULL; static FILE *fsw = NULL; +static FILE *few = NULL; static FILE *fsw_ = NULL; static FILE *fmodel = NULL; static FILE *fqmodel = NULL; +static FILE *fpwb = NULL; static FILE *fpw = NULL; +static FILE *frw = NULL; static FILE *flsp = NULL; +static FILE *fweights = NULL; +static FILE *flsp_ = NULL; static FILE *fphase = NULL; static FILE *fphase_ = NULL; static FILE *ffw = NULL; @@ -48,9 +54,13 @@ static FILE *fe = NULL; static FILE *fsq = NULL; static FILE *fdec = NULL; static FILE *fsnr = NULL; +static FILE *flpcsnr = NULL; static FILE *fak = NULL; +static FILE *fak_ = NULL; static FILE *fbg = NULL; static FILE *fE = NULL; +static FILE *frk = NULL; +static FILE *fhephase = NULL; static char prefix[MAX_STR]; @@ -66,14 +76,24 @@ void dump_off(){ fclose(fsw); if (fsw_ != NULL) fclose(fsw_); + if (few != NULL) + fclose(few); if (fmodel != NULL) fclose(fmodel); if (fqmodel != NULL) fclose(fqmodel); + if (fpwb != NULL) + fclose(fpwb); if (fpw != NULL) fclose(fpw); + if (frw != NULL) + fclose(frw); if (flsp != NULL) fclose(flsp); + if (fweights != NULL) + fclose(fweights); + if (flsp_ != NULL) + fclose(flsp_); if (fphase != NULL) fclose(fphase); if (fphase_ != NULL) @@ -88,12 +108,20 @@ void dump_off(){ fclose(fdec); if (fsnr != NULL) fclose(fsnr); + if (flpcsnr != NULL) + fclose(flpcsnr); if (fak != NULL) fclose(fak); + if (fak_ != NULL) + fclose(fak_); if (fbg != NULL) fclose(fbg); if (fE != NULL) fclose(fE); + if (frk != NULL) + fclose(frk); + if (fhephase != NULL) + fclose(fhephase); } void dump_Sn(float Sn[]) { @@ -155,6 +183,24 @@ void dump_Sw_(COMP Sw_[]) { fprintf(fsw_,"\n"); } +void dump_Ew(COMP Ew[]) { + int i; + char s[MAX_STR]; + + if (!dumpon) return; + + if (few == NULL) { + sprintf(s,"%s_ew.txt", prefix); + few = fopen(s, "wt"); + assert(few != NULL); + } + + for(i=0; i. */ #ifndef __DUMP__ #define __DUMP__ +#include "comp.h" + void dump_on(char filename_prefix[]); void dump_off(); void dump_Sn(float Sn[]); void dump_Sw(COMP Sw[]); void dump_Sw_(COMP Sw_[]); +void dump_Ew(COMP Ew[]); /* amplitude modelling */ void dump_model(MODEL *m); void dump_quantised_model(MODEL *m); +void dump_Pwn(COMP Pw[]); void dump_Pw(COMP Pw[]); +void dump_Rw(float Rw[]); void dump_lsp(float lsp[]); +void dump_weights(float w[], int ndim); +void dump_lsp_(float lsp_[]); void dump_ak(float ak[], int order); +void dump_ak_(float ak[], int order); void dump_E(float E); +void dump_lpc_snr(float snr); /* phase modelling */ void dump_snr(float snr); void dump_phase(float phase[], int L); void dump_phase_(float phase[], int L); +void dump_hephase(int ind[], int dim); /* NLP states */ @@ -55,6 +64,7 @@ void dump_sq(float sq[]); void dump_dec(COMP Fw[]); void dump_Fw(COMP Fw[]); void dump_e(float e_hz[]); +void dump_Rk(float Rk[]); /* post filter */ diff --git a/libs/libcodec2/src/fdmdv.c b/libs/libcodec2/src/fdmdv.c new file mode 100644 index 0000000000..a6204f411e --- /dev/null +++ b/libs/libcodec2/src/fdmdv.c @@ -0,0 +1,1500 @@ +/*---------------------------------------------------------------------------*\ + + FILE........: fdmdv.c + AUTHOR......: David Rowe + DATE CREATED: April 14 2012 + + Functions that implement the FDMDV modem. + +\*---------------------------------------------------------------------------*/ + +/* + Copyright (C) 2012 David Rowe + + All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 2.1, as + published by the Free Software Foundation. This program is + distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program; if not, see . +*/ + +/*---------------------------------------------------------------------------*\ + + INCLUDES + +\*---------------------------------------------------------------------------*/ + +#include +#include +#include +#include +#include + +#include "fdmdv_internal.h" +#include "fdmdv.h" +#include "rn.h" +#include "test_bits.h" +#include "pilot_coeff.h" +#include "kiss_fft.h" +#include "hanning.h" +#include "os.h" + +/*---------------------------------------------------------------------------*\ + + FUNCTIONS + +\*---------------------------------------------------------------------------*/ + +static COMP cneg(COMP a) +{ + COMP res; + + res.real = -a.real; + res.imag = -a.imag; + + return res; +} + +static COMP cconj(COMP a) +{ + COMP res; + + res.real = a.real; + res.imag = -a.imag; + + return res; +} + +static COMP cmult(COMP a, COMP b) +{ + COMP res; + + res.real = a.real*b.real - a.imag*b.imag; + res.imag = a.real*b.imag + a.imag*b.real; + + return res; +} + +static COMP fcmult(float a, COMP b) +{ + COMP res; + + res.real = a*b.real; + res.imag = a*b.imag; + + return res; +} + +static COMP cadd(COMP a, COMP b) +{ + COMP res; + + res.real = a.real + b.real; + res.imag = a.imag + b.imag; + + return res; +} + +static float cabsolute(COMP a) +{ + return sqrt(pow(a.real, 2.0) + pow(a.imag, 2.0)); +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: fdmdv_create + AUTHOR......: David Rowe + DATE CREATED: 16/4/2012 + + Create and initialise an instance of the modem. Returns a pointer + to the modem states or NULL on failure. One set of states is + sufficient for a full duplex modem. + +\*---------------------------------------------------------------------------*/ + +struct FDMDV * CODEC2_WIN32SUPPORT fdmdv_create(void) +{ + struct FDMDV *f; + int c, i, k; + float carrier_freq; + + assert(FDMDV_BITS_PER_FRAME == NC*NB); + assert(FDMDV_NOM_SAMPLES_PER_FRAME == M); + assert(FDMDV_MAX_SAMPLES_PER_FRAME == (M+M/P)); + + f = (struct FDMDV*)malloc(sizeof(struct FDMDV)); + if (f == NULL) + return NULL; + + f->current_test_bit = 0; + for(i=0; irx_test_bits_mem[i] = 0; + + f->tx_pilot_bit = 0; + + for(c=0; cprev_tx_symbols[c].real = 1.0; + f->prev_tx_symbols[c].imag = 0.0; + f->prev_rx_symbols[c].real = 1.0; + f->prev_rx_symbols[c].imag = 0.0; + + for(k=0; ktx_filter_memory[c][k].real = 0.0; + f->tx_filter_memory[c][k].imag = 0.0; + } + + for(k=0; krx_filter_memory[c][k].real = 0.0; + f->rx_filter_memory[c][k].imag = 0.0; + } + + /* Spread initial FDM carrier phase out as far as possible. + This helped PAPR for a few dB. We don't need to adjust rx + phase as DQPSK takes care of that. */ + + f->phase_tx[c].real = cos(2.0*PI*c/(NC+1)); + f->phase_tx[c].imag = sin(2.0*PI*c/(NC+1)); + + f->phase_rx[c].real = 1.0; + f->phase_rx[c].imag = 0.0; + + for(k=0; krx_filter_mem_timing[c][k].real = 0.0; + f->rx_filter_mem_timing[c][k].imag = 0.0; + } + for(k=0; krx_baseband_mem_timing[c][k].real = 0.0; + f->rx_baseband_mem_timing[c][k].imag = 0.0; + } + } + + /* Set up frequency of each carrier */ + + for(c=0; cfreq[c].real = cos(2.0*PI*carrier_freq/FS); + f->freq[c].imag = sin(2.0*PI*carrier_freq/FS); + } + + for(c=NC/2; cfreq[c].real = cos(2.0*PI*carrier_freq/FS); + f->freq[c].imag = sin(2.0*PI*carrier_freq/FS); + } + + f->freq[NC].real = cos(2.0*PI*FDMDV_FCENTRE/FS); + f->freq[NC].imag = sin(2.0*PI*FDMDV_FCENTRE/FS); + + /* Generate DBPSK pilot Look Up Table (LUT) */ + + generate_pilot_lut(f->pilot_lut, &f->freq[NC]); + + /* freq Offset estimation states */ + + f->fft_pilot_cfg = kiss_fft_alloc (MPILOTFFT, 0, NULL, NULL); + assert(f->fft_pilot_cfg != NULL); + + for(i=0; ipilot_baseband1[i].real = f->pilot_baseband2[i].real = 0.0; + f->pilot_baseband1[i].imag = f->pilot_baseband2[i].imag = 0.0; + } + f->pilot_lut_index = 0; + f->prev_pilot_lut_index = 3*M; + + for(i=0; ipilot_lpf1[i].real = f->pilot_lpf2[i].real = 0.0; + f->pilot_lpf1[i].imag = f->pilot_lpf2[i].imag = 0.0; + } + + f->foff = 0.0; + f->foff_rect.real = 1.0; + f->foff_rect.imag = 0.0; + f->foff_phase_rect.real = 1.0; + f->foff_phase_rect.imag = 0.0; + + f->fest_state = 0; + f->coarse_fine = COARSE; + + for(c=0; csig_est[c] = 0.0; + f->noise_est[c] = 0.0; + } + + for(i=0; i<2*FDMDV_NSPEC; i++) + f->fft_buf[i] = 0.0; + f->fft_cfg = kiss_fft_alloc (2*FDMDV_NSPEC, 0, NULL, NULL); + assert(f->fft_cfg != NULL); + + + return f; +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: fdmdv_destroy + AUTHOR......: David Rowe + DATE CREATED: 16/4/2012 + + Destroy an instance of the modem. + +\*---------------------------------------------------------------------------*/ + +void CODEC2_WIN32SUPPORT fdmdv_destroy(struct FDMDV *fdmdv) +{ + assert(fdmdv != NULL); + KISS_FFT_FREE(fdmdv->fft_pilot_cfg); + KISS_FFT_FREE(fdmdv->fft_cfg); + free(fdmdv); +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: fdmdv_get_test_bits() + AUTHOR......: David Rowe + DATE CREATED: 16/4/2012 + + Generate a frame of bits from a repeating sequence of random data. OK so + it's not very random if it repeats but it makes syncing at the demod easier + for test purposes. + +\*---------------------------------------------------------------------------*/ + +void CODEC2_WIN32SUPPORT fdmdv_get_test_bits(struct FDMDV *f, int tx_bits[]) +{ + int i; + + for(i=0; icurrent_test_bit]; + f->current_test_bit++; + if (f->current_test_bit > (NTEST_BITS-1)) + f->current_test_bit = 0; + } + } + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: bits_to_dqpsk_symbols() + AUTHOR......: David Rowe + DATE CREATED: 16/4/2012 + + Maps bits to parallel DQPSK symbols. Generate Nc+1 QPSK symbols from + vector of (1,Nc*Nb) input tx_bits. The Nc+1 symbol is the +1 -1 +1 + .... BPSK sync carrier. + +\*---------------------------------------------------------------------------*/ + +void bits_to_dqpsk_symbols(COMP tx_symbols[], COMP prev_tx_symbols[], int tx_bits[], int *pilot_bit) +{ + int c, msb, lsb; + COMP j = {0.0,1.0}; + + /* map tx_bits to to Nc DQPSK symbols */ + + for(c=0; cprev_tx_symbols, tx_bits, &fdmdv->tx_pilot_bit); + memcpy(fdmdv->prev_tx_symbols, tx_symbols, sizeof(COMP)*(NC+1)); + tx_filter(tx_baseband, tx_symbols, fdmdv->tx_filter_memory); + fdm_upconvert(tx_fdm, tx_baseband, fdmdv->phase_tx, fdmdv->freq); + + *sync_bit = fdmdv->tx_pilot_bit; +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: generate_pilot_fdm() + AUTHOR......: David Rowe + DATE CREATED: 19/4/2012 + + Generate M samples of DBPSK pilot signal for Freq offset estimation. + +\*---------------------------------------------------------------------------*/ + +void generate_pilot_fdm(COMP *pilot_fdm, int *bit, float *symbol, + float *filter_mem, COMP *phase, COMP *freq) +{ + int i,j,k; + float tx_baseband[M]; + + /* +1 -1 +1 -1 DBPSK sync carrier, once filtered becomes (roughly) + two spectral lines at +/- RS/2 */ + + if (*bit) + *symbol = -*symbol; + else + *symbol = *symbol; + if (*bit) + *bit = 0; + else + *bit = 1; + + /* filter DPSK symbol to create M baseband samples */ + + filter_mem[NFILTER-1] = (sqrt(2)/2) * *symbol; + for(i=0; ireal; + pilot_fdm[i].imag = sqrt(2)*2*tx_baseband[i] * phase->imag; + } +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: generate_pilot_lut() + AUTHOR......: David Rowe + DATE CREATED: 19/4/2012 + + Generate a 4M sample vector of DBPSK pilot signal. As the pilot signal + is periodic in 4M samples we can then use this vector as a look up table + for pilot signal generation in the demod. + +\*---------------------------------------------------------------------------*/ + +void generate_pilot_lut(COMP pilot_lut[], COMP *pilot_freq) +{ + int pilot_rx_bit = 0; + float pilot_symbol = sqrt(2.0); + COMP pilot_phase = {1.0, 0.0}; + float pilot_filter_mem[NFILTER]; + COMP pilot[M]; + int i,f; + + for(i=0; i= 4) + memcpy(&pilot_lut[M*(f-4)], pilot, M*sizeof(COMP)); + } + +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: lpf_peak_pick() + AUTHOR......: David Rowe + DATE CREATED: 20/4/2012 + + LPF and peak pick part of freq est, put in a function as we call it twice. + +\*---------------------------------------------------------------------------*/ + +void lpf_peak_pick(float *foff, float *max, COMP pilot_baseband[], + COMP pilot_lpf[], kiss_fft_cfg fft_pilot_cfg, COMP S[], int nin) +{ + int i,j,k; + int mpilot; + COMP s[MPILOTFFT]; + float mag, imax; + int ix; + float r; + + /* LPF cutoff 200Hz, so we can handle max +/- 200 Hz freq offset */ + + for(i=0; i imax) { + imax = mag; + ix = i; + } + } + r = 2.0*200.0/MPILOTFFT; /* maps FFT bin to frequency in Hz */ + + if (ix >= MPILOTFFT/2) + *foff = (ix - MPILOTFFT)*r; + else + *foff = (ix)*r; + *max = imax; + +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: rx_est_freq_offset() + AUTHOR......: David Rowe + DATE CREATED: 19/4/2012 + + Estimate frequency offset of FDM signal using BPSK pilot. Note that + this algorithm is quite sensitive to pilot tone level wrt other + carriers, so test variations to the pilot amplitude carefully. + +\*---------------------------------------------------------------------------*/ + +float rx_est_freq_offset(struct FDMDV *f, COMP rx_fdm[], int nin) +{ + int i,j; + COMP pilot[M+M/P]; + COMP prev_pilot[M+M/P]; + float foff, foff1, foff2; + float max1, max2; + + assert(nin <= M+M/P); + + /* get pilot samples used for correlation/down conversion of rx signal */ + + for (i=0; ipilot_lut[f->pilot_lut_index]; + f->pilot_lut_index++; + if (f->pilot_lut_index >= 4*M) + f->pilot_lut_index = 0; + + prev_pilot[i] = f->pilot_lut[f->prev_pilot_lut_index]; + f->prev_pilot_lut_index++; + if (f->prev_pilot_lut_index >= 4*M) + f->prev_pilot_lut_index = 0; + } + + /* + Down convert latest M samples of pilot by multiplying by ideal + BPSK pilot signal we have generated locally. The peak of the + resulting signal is sensitive to the time shift between the + received and local version of the pilot, so we do it twice at + different time shifts and choose the maximum. + */ + + for(i=0; ipilot_baseband1[i] = f->pilot_baseband1[i+nin]; + f->pilot_baseband2[i] = f->pilot_baseband2[i+nin]; + } + + for(i=0,j=NPILOTBASEBAND-nin; ipilot_baseband1[j] = cmult(rx_fdm[i], cconj(pilot[i])); + f->pilot_baseband2[j] = cmult(rx_fdm[i], cconj(prev_pilot[i])); + } + + lpf_peak_pick(&foff1, &max1, f->pilot_baseband1, f->pilot_lpf1, f->fft_pilot_cfg, f->S1, nin); + lpf_peak_pick(&foff2, &max2, f->pilot_baseband2, f->pilot_lpf2, f->fft_pilot_cfg, f->S2, nin); + + if (max1 > max2) + foff = foff1; + else + foff = foff2; + + return foff; +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: fdmdv_freq_shift() + AUTHOR......: David Rowe + DATE CREATED: 26/4/2012 + + Frequency shift modem signal. The use of complex input and output allows + single sided frequency shifting (no images). + +\*---------------------------------------------------------------------------*/ + +void CODEC2_WIN32SUPPORT fdmdv_freq_shift(COMP rx_fdm_fcorr[], COMP rx_fdm[], float foff, + COMP *foff_rect, COMP *foff_phase_rect, int nin) +{ + int i; + + foff_rect->real = cos(2.0*PI*foff/FS); + foff_rect->imag = sin(2.0*PI*foff/FS); + for(i=0; ireal /= cabsolute(*foff_phase_rect); + foff_phase_rect->imag /= cabsolute(*foff_phase_rect); +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: fdm_downconvert() + AUTHOR......: David Rowe + DATE CREATED: 22/4/2012 + + Frequency shift each modem carrier down to Nc+1 baseband signals. + +\*---------------------------------------------------------------------------*/ + +void fdm_downconvert(COMP rx_baseband[NC+1][M+M/P], COMP rx_fdm[], COMP phase_rx[], COMP freq[], int nin) +{ + int i,c; + + /* maximum number of input samples to demod */ + + assert(nin <= (M+M/P)); + + /* Nc/2 tones below centre freq */ + + for (c=0; c M) + rx_timing -= M; + if (rx_timing < -M) + rx_timing += M; + + /* rx_filt_mem_timing contains M + Nfilter + M samples of the + baseband signal at rate M this enables us to resample the + filtered rx symbol with M sample precision once we have + rx_timing */ + + for(c=0; c= 0) && (d.imag >= 0)) { + msb = 0; lsb = 0; + } + if ((d.real < 0) && (d.imag >= 0)) { + msb = 0; lsb = 1; + } + if ((d.real < 0) && (d.imag < 0)) { + msb = 1; lsb = 0; + } + if ((d.real >= 0) && (d.imag < 0)) { + msb = 1; lsb = 1; + } + rx_bits[2*c] = msb; + rx_bits[2*c+1] = lsb; + } + + /* Extract DBPSK encoded Sync bit and fine freq offset estimate */ + + phase_difference[NC] = cmult(rx_symbols[NC], cconj(prev_rx_symbols[NC])); + if (phase_difference[NC].real < 0) { + *sync_bit = 1; + ferr = phase_difference[NC].imag; + } + else { + *sync_bit = 0; + ferr = -phase_difference[NC].imag; + } + + /* pilot carrier gets an extra pi/4 rotation to make it consistent + with other carriers, as we need it for snr_update and scatter + diagram */ + + phase_difference[NC] = cmult(phase_difference[NC], pi_on_4); + + return ferr; +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: snr_update() + AUTHOR......: David Rowe + DATE CREATED: 17 May 2012 + + Given phase differences update estimates of signal and noise levels. + +\*---------------------------------------------------------------------------*/ + +void snr_update(float sig_est[], float noise_est[], COMP phase_difference[]) +{ + float s[NC+1]; + COMP refl_symbols[NC+1]; + float n[NC+1]; + COMP pi_on_4; + int c; + + pi_on_4.real = cos(PI/4.0); + pi_on_4.imag = sin(PI/4.0); + + /* mag of each symbol is distance from origin, this gives us a + vector of mags, one for each carrier. */ + + for(c=0; crx_test_bits_mem[i] = f->rx_test_bits_mem[j]; + for(i=NTEST_BITS-FDMDV_BITS_PER_FRAME,j=0; irx_test_bits_mem[i] = rx_bits[j]; + + /* see how many bit errors we get when checked against test sequence */ + + *bit_errors = 0; + for(i=0; irx_test_bits_mem[i]; + //printf("%d %d %d %d\n", i, test_bits[i], f->rx_test_bits_mem[i], test_bits[i] ^ f->rx_test_bits_mem[i]); + } + + /* if less than a thresh we are aligned and in sync with test sequence */ + + ber = (float)*bit_errors/NTEST_BITS; + + *sync = 0; + if (ber < 0.2) + *sync = 1; + + *ntest_bits = NTEST_BITS; + +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: freq_state(() + AUTHOR......: David Rowe + DATE CREATED: 24/4/2012 + + Freq offset state machine. Moves between coarse and fine states + based on BPSK pilot sequence. Freq offset estimator occasionally + makes mistakes when used continuously. So we use it until we have + acquired the BPSK pilot, then switch to a more robust "fine" + tracking algorithm. If we lose sync we switch back to coarse mode + for fast re-acquisition of large frequency offsets. + +\*---------------------------------------------------------------------------*/ + +int freq_state(int sync_bit, int *state) +{ + int next_state, coarse_fine; + + /* acquire state, look for 6 symbol 010101 sequence from sync bit */ + + next_state = *state; + switch(*state) { + case 0: + if (sync_bit == 0) + next_state = 1; + break; + case 1: + if (sync_bit == 1) + next_state = 2; + else + next_state = 0; + break; + case 2: + if (sync_bit == 0) + next_state = 3; + else + next_state = 0; + break; + case 3: + if (sync_bit == 1) + next_state = 4; + else + next_state = 0; + break; + case 4: + if (sync_bit == 0) + next_state = 5; + else + next_state = 0; + break; + case 5: + if (sync_bit == 1) + next_state = 6; + else + next_state = 0; + break; + + /* states 6 and above are track mode, make sure we keep + getting 0101 sync bit sequence */ + + case 6: + if (sync_bit == 0) + next_state = 7; + else + next_state = 0; + + break; + case 7: + if (sync_bit == 1) + next_state = 6; + else + next_state = 0; + break; + } + + *state = next_state; + if (*state >= 6) + coarse_fine = FINE; + else + coarse_fine = COARSE; + + return coarse_fine; +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: fdmdv_demod() + AUTHOR......: David Rowe + DATE CREATED: 26/4/2012 + + FDMDV demodulator, take an array of FDMDV_SAMPLES_PER_FRAME + modulated samples, returns an array of FDMDV_BITS_PER_FRAME bits, + plus the sync bit. + + The input signal is complex to support single sided frequcny shifting + before the demod input (e.g. fdmdv2 click to tune feature). + + The number of input samples nin will normally be M == + FDMDV_SAMPLES_PER_FRAME. However to adjust for differences in + transmit and receive sample clocks nin will occasionally be M-M/P, + or M+M/P. + +\*---------------------------------------------------------------------------*/ + +void CODEC2_WIN32SUPPORT fdmdv_demod(struct FDMDV *fdmdv, int rx_bits[], + int *sync_bit, COMP rx_fdm[], int *nin) +{ + float foff_coarse, foff_fine; + COMP rx_fdm_fcorr[M+M/P]; + COMP rx_baseband[NC+1][M+M/P]; + COMP rx_filt[NC+1][P+1]; + COMP rx_symbols[NC+1]; + float env[NT*P]; + + /* freq offset estimation and correction */ + + foff_coarse = rx_est_freq_offset(fdmdv, rx_fdm, *nin); + + if (fdmdv->coarse_fine == COARSE) + fdmdv->foff = foff_coarse; + fdmdv_freq_shift(rx_fdm_fcorr, rx_fdm, -fdmdv->foff, &fdmdv->foff_rect, &fdmdv->foff_phase_rect, *nin); + + /* baseband processing */ + + fdm_downconvert(rx_baseband, rx_fdm_fcorr, fdmdv->phase_rx, fdmdv->freq, *nin); + rx_filter(rx_filt, rx_baseband, fdmdv->rx_filter_memory, *nin); + fdmdv->rx_timing = rx_est_timing(rx_symbols, rx_filt, rx_baseband, fdmdv->rx_filter_mem_timing, env, fdmdv->rx_baseband_mem_timing, *nin); + + /* Adjust number of input samples to keep timing within bounds */ + + *nin = M; + + if (fdmdv->rx_timing > 2*M/P) + *nin += M/P; + + if (fdmdv->rx_timing < 0) + *nin -= M/P; + + foff_fine = qpsk_to_bits(rx_bits, sync_bit, fdmdv->phase_difference, fdmdv->prev_rx_symbols, rx_symbols); + memcpy(fdmdv->prev_rx_symbols, rx_symbols, sizeof(COMP)*(NC+1)); + snr_update(fdmdv->sig_est, fdmdv->noise_est, fdmdv->phase_difference); + + /* freq offset estimation state machine */ + + fdmdv->coarse_fine = freq_state(*sync_bit, &fdmdv->fest_state); + fdmdv->foff -= TRACK_COEFF*foff_fine; +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: calc_snr() + AUTHOR......: David Rowe + DATE CREATED: 17 May 2012 + + Calculate current SNR estimate (3000Hz noise BW) + +\*---------------------------------------------------------------------------*/ + +float calc_snr(float sig_est[], float noise_est[]) +{ + float S, SdB; + float mean, N50, N50dB, N3000dB; + float snr_dB; + int c; + + S = 0.0; + for(c=0; csnr_est = calc_snr(fdmdv->sig_est, fdmdv->noise_est); + fdmdv_stats->fest_coarse_fine = fdmdv->coarse_fine; + fdmdv_stats->foff = fdmdv->foff; + fdmdv_stats->rx_timing = fdmdv->rx_timing; + fdmdv_stats->clock_offset = 0.0; /* TODO - implement clock offset estimation */ + + assert((NC+1) == FDMDV_NSYM); + + for(c=0; crx_symbols[c] = fdmdv->phase_difference[c]; + } +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: fdmdv_8_to_48() + AUTHOR......: David Rowe + DATE CREATED: 9 May 2012 + + Changes the sample rate of a signal from 8 to 48 kHz. Experience + with PC based modems has shown that PC sound cards have a more + accurate sample clock when set for 48 kHz than 8 kHz. + + n is the number of samples at the 8 kHz rate, there are FDMDV_OS*n samples + at the 48 kHz rate. A memory of FDMDV_OS_TAPS/FDMDV_OS samples is reqd for + in8k[] (see t48_8.c unit test as example). + + This is a classic polyphase upsampler. We take the 8 kHz samples + and insert (FDMDV_OS-1) zeroes between each sample, then + FDMDV_OS_TAPS FIR low pass filter the signal at 4kHz. As most of + the input samples are zeroes, we only need to multiply non-zero + input samples by filter coefficients. The zero insertion and + filtering are combined in the code below and I'm too lazy to explain + it further right now.... + +\*---------------------------------------------------------------------------*/ + +void CODEC2_WIN32SUPPORT fdmdv_8_to_48(float out48k[], float in8k[], int n) +{ + int i,j,k,l; + + /* make sure n is an integer multiple of the oversampling rate, ow + this function breaks */ + + assert((n % FDMDV_OS) == 0); + + for(i=0; ifft_buf[i] = f->fft_buf[i+nin]; + for(j=0; jfft_buf[i] = rx_fdm[j].real; + assert(i == 2*FDMDV_NSPEC); + + /* window and FFT */ + + for(i=0; i<2*FDMDV_NSPEC; i++) { + fft_in[i].real = f->fft_buf[i] * (0.5 - 0.5*cos((float)i*2.0*PI/(2*FDMDV_NSPEC))); + fft_in[i].imag = 0.0; + } + + kiss_fft(f->fft_cfg, (kiss_fft_cpx *)fft_in, (kiss_fft_cpx *)fft_out); + + /* FFT scales up a signal of level 1 FDMDV_NSPEC */ + + full_scale_dB = 20*log10(FDMDV_NSPEC); + + /* scale and convert to dB */ + + for(i=0; iphase_tx[i])); + fprintf(stderr,"\nfreq[]:\n"); + for(i=0; i<=NC; i++) + fprintf(stderr," %1.3f", cabsolute(f->freq[i])); + fprintf(stderr,"\nfoff_rect %1.3f foff_phase_rect: %1.3f", cabsolute(f->foff_rect), cabsolute(f->foff_phase_rect)); + fprintf(stderr,"\nphase_rx[]:\n"); + for(i=0; i<=NC; i++) + fprintf(stderr," %1.3f", cabsolute(f->phase_rx[i])); + fprintf(stderr, "\n\n"); +} diff --git a/libs/libcodec2/src/fdmdv.h b/libs/libcodec2/src/fdmdv.h new file mode 100644 index 0000000000..3ad83e6d4f --- /dev/null +++ b/libs/libcodec2/src/fdmdv.h @@ -0,0 +1,114 @@ +/*---------------------------------------------------------------------------*\ + + FILE........: fdmdv.h + AUTHOR......: David Rowe + DATE CREATED: April 14 2012 + + A 1400 bit/s Frequency Division Multiplexed Digital Voice (FDMDV) + modem. Used for digital audio over HF SSB. See README_fdmdv.txt for + more information, and fdmdv_mod.c and fdmdv_demod.c for example + usage. + + References: + + [1] http://n1su.com/fdmdv/FDMDV_Docs_Rel_1_4b.pdf + +\*---------------------------------------------------------------------------*/ + +/* + Copyright (C) 2012 David Rowe + + All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 2.1, as + published by the Free Software Foundation. This program is + distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program; if not, see . +*/ + +#ifndef __FDMDV__ +#define __FDMDV__ + +#ifdef __cplusplus +extern "C" { +#endif + +/* set up the calling convention for DLL function import/export for + WIN32 cross compiling */ + +#ifdef __CODEC2_WIN32__ +#ifdef __CODEC2_BUILDING_DLL__ +#define CODEC2_WIN32SUPPORT __declspec(dllexport) __stdcall +#else +#define CODEC2_WIN32SUPPORT __declspec(dllimport) __stdcall +#endif +#else +#define CODEC2_WIN32SUPPORT +#endif + +#include "comp.h" + +#define FDMDV_BITS_PER_FRAME 28 /* 20ms frames, 1400 bit/s */ +#define FDMDV_NOM_SAMPLES_PER_FRAME 160 /* modulator output samples/frame and nominal demod samples/frame */ + /* at 8000 Hz sample rate */ +#define FDMDV_MAX_SAMPLES_PER_FRAME 200 /* max demod samples/frame, use this to allocate storage */ +#define FDMDV_SCALE 1000 /* suggested scaling for 16 bit shorts */ +#define FDMDV_NSYM 15 +#define FDMDV_FCENTRE 1500 /* Centre frequency, Nc/2 carriers below this, Nc/2 carriers above (Hz) */ + +/* 8 to 48 kHz sample rate conversion */ + +#define FDMDV_OS 6 /* oversampling rate */ +#define FDMDV_OS_TAPS 48 /* number of OS filter taps */ + +/* FFT points */ + +#define FDMDV_NSPEC 512 +#define FDMDV_MAX_F_HZ 4000 + +/* FDMDV states and stats structures */ + +struct FDMDV; + +struct FDMDV_STATS { + float snr_est; /* estimated SNR of rx signal in dB (3 kHz noise BW) */ + COMP rx_symbols[FDMDV_NSYM]; /* latest received symbols, for scatter plot */ + int fest_coarse_fine; /* freq est state, 0-coarse 1-fine */ + float foff; /* estimated freq offset in Hz */ + float rx_timing; /* estimated optimum timing offset in samples */ + float clock_offset; /* Estimated tx/rx sample clock offset in ppm */ +}; + +struct FDMDV * CODEC2_WIN32SUPPORT fdmdv_create(void); +void CODEC2_WIN32SUPPORT fdmdv_destroy(struct FDMDV *fdmdv_state); + +void CODEC2_WIN32SUPPORT fdmdv_mod(struct FDMDV *fdmdv_state, COMP tx_fdm[], int tx_bits[], int *sync_bit); +void CODEC2_WIN32SUPPORT fdmdv_demod(struct FDMDV *fdmdv_state, int rx_bits[], int *sync_bit, COMP rx_fdm[], int *nin); + +void CODEC2_WIN32SUPPORT fdmdv_get_test_bits(struct FDMDV *fdmdv_state, int tx_bits[]); +void CODEC2_WIN32SUPPORT fdmdv_put_test_bits(struct FDMDV *f, int *sync, int *bit_errors, int *ntest_bits, int rx_bits[]); + +void CODEC2_WIN32SUPPORT fdmdv_get_demod_stats(struct FDMDV *fdmdv_state, struct FDMDV_STATS *fdmdv_stats); +void CODEC2_WIN32SUPPORT fdmdv_get_rx_spectrum(struct FDMDV *fdmdv_state, float mag_dB[], COMP rx_fdm[], int nin); + +void CODEC2_WIN32SUPPORT fdmdv_8_to_48(float out48k[], float in8k[], int n); +void CODEC2_WIN32SUPPORT fdmdv_48_to_8(float out8k[], float in48k[], int n); + +void CODEC2_WIN32SUPPORT fdmdv_freq_shift(COMP rx_fdm_fcorr[], COMP rx_fdm[], float foff, COMP *foff_rect, COMP *foff_phase_rect, int nin); + +/* debug/development function(s) */ + +void CODEC2_WIN32SUPPORT fdmdv_dump_osc_mags(struct FDMDV *f); + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/libs/libcodec2/src/fdmdv_demod.c b/libs/libcodec2/src/fdmdv_demod.c new file mode 100644 index 0000000000..96090d033d --- /dev/null +++ b/libs/libcodec2/src/fdmdv_demod.c @@ -0,0 +1,233 @@ +/*---------------------------------------------------------------------------*\ + + FILE........: fdmdv_demod.c + AUTHOR......: David Rowe + DATE CREATED: April 30 2012 + + Given an input raw file (8kHz, 16 bit shorts) of FDMDV modem samples + outputs a file of bits. The output file is assumed to be arranged + as codec frames of 56 bits (7 bytes) which are received as two 28 + bit modem frames. + + Demod states can be optionally logged to an Octave file for display + using the Octave script fdmdv_demod_c.m. This is useful for + checking demod performance. + +\*---------------------------------------------------------------------------*/ + + +/* + Copyright (C) 2012 David Rowe + + All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 2, as + published by the Free Software Foundation. This program is + distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program; if not, see . +*/ + +#include +#include +#include +#include +#include +#include + +#include "fdmdv.h" +#include "octave.h" + +#define BITS_PER_CODEC_FRAME (2*FDMDV_BITS_PER_FRAME) +#define BYTES_PER_CODEC_FRAME (BITS_PER_CODEC_FRAME/8) + +/* lof of information we want to dump to Octave */ + +#define MAX_FRAMES 50*60 /* 1 minute at 50 symbols/s */ + +int main(int argc, char *argv[]) +{ + FILE *fin, *fout; + struct FDMDV *fdmdv; + char packed_bits[BYTES_PER_CODEC_FRAME]; + int rx_bits[FDMDV_BITS_PER_FRAME]; + int codec_bits[2*FDMDV_BITS_PER_FRAME]; + COMP rx_fdm[FDMDV_MAX_SAMPLES_PER_FRAME]; + short rx_fdm_scaled[FDMDV_MAX_SAMPLES_PER_FRAME]; + int i, bit, byte, c; + int nin, nin_prev; + int sync_bit; + int state, next_state; + int f; + FILE *foct = NULL; + struct FDMDV_STATS stats; + float *rx_fdm_log; + int rx_fdm_log_col_index; + COMP rx_symbols_log[FDMDV_NSYM][MAX_FRAMES]; + int coarse_fine_log[MAX_FRAMES]; + float rx_timing_log[MAX_FRAMES]; + float foff_log[MAX_FRAMES]; + int sync_bit_log[MAX_FRAMES]; + int rx_bits_log[FDMDV_BITS_PER_FRAME*MAX_FRAMES]; + float snr_est_log[MAX_FRAMES]; + float *rx_spec_log; + int max_frames_reached; + + if (argc < 3) { + printf("usage: %s InputModemRawFile OutputBitFile [OctaveDumpFile]\n", argv[0]); + printf("e.g %s hts1a_fdmdv.raw hts1a.c2\n", argv[0]); + exit(1); + } + + if (strcmp(argv[1], "-") == 0) fin = stdin; + else if ( (fin = fopen(argv[1],"rb")) == NULL ) { + fprintf(stderr, "Error opening input modem sample file: %s: %s.\n", + argv[1], strerror(errno)); + exit(1); + } + + if (strcmp(argv[2], "-") == 0) fout = stdout; + else if ( (fout = fopen(argv[2],"wb")) == NULL ) { + fprintf(stderr, "Error opening output bit file: %s: %s.\n", + argv[2], strerror(errno)); + exit(1); + } + + /* malloc some of the bigger variables to prevent out of stack problems */ + + rx_fdm_log = (float*)malloc(sizeof(float)*FDMDV_MAX_SAMPLES_PER_FRAME*MAX_FRAMES); + assert(rx_fdm_log != NULL); + rx_spec_log = (float*)malloc(sizeof(float)*FDMDV_NSPEC*MAX_FRAMES); + assert(rx_spec_log != NULL); + + fdmdv = fdmdv_create(); + f = 0; + state = 0; + nin = FDMDV_NOM_SAMPLES_PER_FRAME; + rx_fdm_log_col_index = 0; + max_frames_reached = 0; + + while(fread(rx_fdm_scaled, sizeof(short), nin, fin) == nin) + { + for(i=0; i. +*/ + +#include +#include +#include +#include +#include +#include + +#include "fdmdv.h" + +#define BITS_PER_CODEC_FRAME (2*FDMDV_BITS_PER_FRAME) +#define BYTES_PER_CODEC_FRAME (BITS_PER_CODEC_FRAME/8) + +int main(int argc, char *argv[]) +{ + FILE *fout; + struct FDMDV *fdmdv; + char packed_bits[BYTES_PER_CODEC_FRAME]; + int tx_bits[2*FDMDV_BITS_PER_FRAME]; + int n, i, bit, byte; + int numBits, nCodecFrames; + + if (argc < 3) { + printf("usage: %s OutputBitFile numBits\n", argv[0]); + printf("e.g %s test.c2 1400\n", argv[0]); + exit(1); + } + + if (strcmp(argv[1], "-") == 0) fout = stdout; + else if ( (fout = fopen(argv[1],"wb")) == NULL ) { + fprintf(stderr, "Error opening output bit file: %s: %s.\n", + argv[1], strerror(errno)); + exit(1); + } + + numBits = atoi(argv[2]); + nCodecFrames = numBits/BITS_PER_CODEC_FRAME; + + fdmdv = fdmdv_create(); + + for(n=0; n. +*/ + +#include +#include +#include +#include +#include +#include + +#include "fdmdv.h" + +#define MAX_INTERLEAVER 1024 + +int main(int argc, char *argv[]) +{ + FILE *fin, *fout, *finter; + int interleaver[MAX_INTERLEAVER]; + char *packed_bits; + int *bits; + int *interleaved_bits; + int i, bit, byte, m, mpacked, frames, interleave, src_bit, dest_bit; + + if (argc < 4) { + printf("usage: %s InputBitFile OutputBitFile InterleaverFile [de]\n", argv[0]); + printf("e.g %s hts1a.c2 hts1a_interleaved.c2 interleaver.txt\n", argv[0]); + exit(1); + } + + if (strcmp(argv[1], "-") == 0) fin = stdin; + else if ( (fin = fopen(argv[1],"rb")) == NULL ) { + fprintf(stderr, "Error opening input bit file: %s: %s.\n", + argv[1], strerror(errno)); + exit(1); + } + + if (strcmp(argv[2], "-") == 0) fout = stdout; + else if ( (fout = fopen(argv[2],"wb")) == NULL ) { + fprintf(stderr, "Error opening output bit file: %s: %s.\n", + argv[2], strerror(errno)); + exit(1); + } + + if ((finter = fopen(argv[3],"rt")) == NULL ) { + fprintf(stderr, "Error opening interleaver file: %s: %s.\n", + argv[3], strerror(errno)); + exit(1); + } + + if (argc == 5) + interleave = 1; + else + interleave = 0; + + /* load interleaver, size determines block size we will process */ + + src_bit = 0; + while(fscanf(finter, "%d\n", &dest_bit) == 1) { + if (interleave) + interleaver[dest_bit] = src_bit; + else + interleaver[src_bit] = dest_bit; + + src_bit++; + if (src_bit == MAX_INTERLEAVER) { + fprintf(stderr, "Error interleaver too big\n"); + exit(1); + } + } + fclose(finter); + + m = src_bit; + fprintf(stderr, "Interleaver size m = %d interleave = %d\n", m, interleave); + mpacked = m/8; + + packed_bits = (char*)malloc(mpacked*sizeof(char)); + assert(packed_bits != NULL); + bits = (int*)malloc(m*sizeof(int)); + assert(bits != NULL); + interleaved_bits = (int*)malloc(m*sizeof(int)); + assert(interleaved_bits != NULL); + + frames = 0; + + while(fread(packed_bits, sizeof(char), mpacked, fin) == mpacked) { + frames++; + + /* unpack bits, MSB first */ + + bit = 7; byte = 0; + for(i=0; i> bit) & 0x1; + bit--; + if (bit < 0) { + bit = 7; + byte++; + } + } + assert(byte == mpacked); + + /* (de) interleave */ + + for(i=0; i. +*/ + +#ifndef __FDMDV_INTERNAL__ +#define __FDMDV_INTERNAL__ + +#include "comp.h" +#include "fdmdv.h" +#include "kiss_fft.h" + +/*---------------------------------------------------------------------------*\ + + DEFINES + +\*---------------------------------------------------------------------------*/ + +#define PI 3.141592654 +#define FS 8000 /* sample rate in Hz */ +#define T (1.0/FS) /* sample period in seconds */ +#define RS 50 /* symbol rate in Hz */ +#define NC 14 /* number of data carriers (plus one pilot in the centre) */ +#define NB 2 /* Bits/symbol for QPSK modulation */ +#define RB (NC*RS*NB) /* bit rate */ +#define M (FS/RS) /* oversampling factor */ +#define NSYM 6 /* number of symbols to filter over */ +#define NFILTER (NSYM*M) /* size of tx/rx filters at sample rate M */ + +#define FSEP 75 /* Separation between carriers (Hz) */ + +#define NT 5 /* number of symbols we estimate timing over */ +#define P 4 /* oversample factor used for initial rx symbol filtering */ +#define NFILTERTIMING (M+NFILTER+M) /* filter memory used for resampling after timing estimation */ + +#define NTEST_BITS (NC*NB*4) /* length of test bit sequence */ + +#define NPILOT_LUT (4*M) /* number of pilot look up table samples */ +#define NPILOTCOEFF 30 /* number of FIR filter coeffs in LP filter */ +#define NPILOTBASEBAND (NPILOTCOEFF+M+M/P) /* number of pilot baseband samples reqd for pilot LPF */ +#define NPILOTLPF (4*M) /* number of samples we DFT pilot over, pilot est window */ +#define MPILOTFFT 256 + +/* freq offset sestimation states */ + +#define COARSE 0 +#define FINE 1 + +/* averaging filter coeffs */ + +#define TRACK_COEFF 0.5 +#define SNR_COEFF 0.9 /* SNR est averaging filter coeff */ + +/*---------------------------------------------------------------------------*\ + + STRUCT for States + +\*---------------------------------------------------------------------------*/ + +struct FDMDV { + /* test data (test frame) states */ + + int current_test_bit; + int rx_test_bits_mem[NTEST_BITS]; + + /* Modulator */ + + int tx_pilot_bit; + COMP prev_tx_symbols[NC+1]; + COMP tx_filter_memory[NC+1][NSYM]; + COMP phase_tx[NC+1]; + COMP freq[NC+1]; + + /* Pilot generation at demodulator */ + + COMP pilot_lut[NPILOT_LUT]; + int pilot_lut_index; + int prev_pilot_lut_index; + + /* freq offset estimation states */ + + kiss_fft_cfg fft_pilot_cfg; + COMP pilot_baseband1[NPILOTBASEBAND]; + COMP pilot_baseband2[NPILOTBASEBAND]; + COMP pilot_lpf1[NPILOTLPF]; + COMP pilot_lpf2[NPILOTLPF]; + COMP S1[MPILOTFFT]; + COMP S2[MPILOTFFT]; + + /* freq offset correction states */ + + float foff; + COMP foff_rect; + COMP foff_phase_rect; + + /* Demodulator */ + + COMP phase_rx[NC+1]; + COMP rx_filter_memory[NC+1][NFILTER]; + COMP rx_filter_mem_timing[NC+1][NT*P]; + COMP rx_baseband_mem_timing[NC+1][NFILTERTIMING]; + float rx_timing; + COMP phase_difference[NC+1]; + COMP prev_rx_symbols[NC+1]; + + /* freq est state machine */ + + int fest_state; + int coarse_fine; + + /* SNR estimation states */ + + float sig_est[NC+1]; + float noise_est[NC+1]; + + /* Buf for FFT/waterfall */ + + float fft_buf[2*FDMDV_NSPEC]; + kiss_fft_cfg fft_cfg; + }; + +/*---------------------------------------------------------------------------*\ + + FUNCTION PROTOTYPES + +\*---------------------------------------------------------------------------*/ + +void bits_to_dqpsk_symbols(COMP tx_symbols[], COMP prev_tx_symbols[], int tx_bits[], int *pilot_bit); +void tx_filter(COMP tx_baseband[NC+1][M], COMP tx_symbols[], COMP tx_filter_memory[NC+1][NSYM]); +void fdm_upconvert(COMP tx_fdm[], COMP tx_baseband[NC+1][M], COMP phase_tx[], COMP freq_tx[]); +void generate_pilot_fdm(COMP *pilot_fdm, int *bit, float *symbol, float *filter_mem, COMP *phase, COMP *freq); +void generate_pilot_lut(COMP pilot_lut[], COMP *pilot_freq); +float rx_est_freq_offset(struct FDMDV *f, COMP rx_fdm[], int nin); +void lpf_peak_pick(float *foff, float *max, COMP pilot_baseband[], COMP pilot_lpf[], kiss_fft_cfg fft_pilot_cfg, COMP S[], int nin); +void freq_shift(COMP rx_fdm_fcorr[], COMP rx_fdm[], float foff, COMP *foff_rect, COMP *foff_phase_rect, int nin); +void fdm_downconvert(COMP rx_baseband[NC+1][M+M/P], COMP rx_fdm[], COMP phase_rx[], COMP freq[], int nin); +void rx_filter(COMP rx_filt[NC+1][P+1], COMP rx_baseband[NC+1][M+M/P], COMP rx_filter_memory[NC+1][NFILTER], int nin); +float rx_est_timing(COMP rx_symbols[], + COMP rx_filt[NC+1][P+1], + COMP rx_baseband[NC+1][M+M/P], + COMP rx_filter_mem_timing[NC+1][NT*P], + float env[], + COMP rx_baseband_mem_timing[NC+1][NFILTERTIMING], + int nin); +float qpsk_to_bits(int rx_bits[], int *sync_bit, COMP phase_difference[], COMP prev_rx_symbols[], COMP rx_symbols[]); +void snr_update(float sig_est[], float noise_est[], COMP phase_difference[]); +int freq_state(int sync_bit, int *state); +float calc_snr(float sig_est[], float noise_est[]); + +#endif diff --git a/libs/libcodec2/src/fdmdv_mod.c b/libs/libcodec2/src/fdmdv_mod.c new file mode 100644 index 0000000000..b85f8d1fa4 --- /dev/null +++ b/libs/libcodec2/src/fdmdv_mod.c @@ -0,0 +1,124 @@ +/*---------------------------------------------------------------------------*\ + + FILE........: fdmdv_mod.c + AUTHOR......: David Rowe + DATE CREATED: April 28 2012 + + Given an input file of bits outputs a raw file (8kHz, 16 bit shorts) + of FDMDV modem samples ready to send over a HF radio channel. The + input file is assumed to be arranged as codec frames of 56 bits (7 + bytes) which we send as two 28 bit modem frames. + +\*---------------------------------------------------------------------------*/ + + +/* + Copyright (C) 2012 David Rowe + + All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 2, as + published by the Free Software Foundation. This program is + distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program; if not, see . +*/ + +#include +#include +#include +#include +#include +#include + +#include "fdmdv.h" + +#define BITS_PER_CODEC_FRAME (2*FDMDV_BITS_PER_FRAME) +#define BYTES_PER_CODEC_FRAME (BITS_PER_CODEC_FRAME/8) + +int main(int argc, char *argv[]) +{ + FILE *fin, *fout; + struct FDMDV *fdmdv; + char packed_bits[BYTES_PER_CODEC_FRAME]; + int tx_bits[2*FDMDV_BITS_PER_FRAME]; + COMP tx_fdm[2*FDMDV_NOM_SAMPLES_PER_FRAME]; + short tx_fdm_scaled[2*FDMDV_NOM_SAMPLES_PER_FRAME]; + int frames; + int i, bit, byte; + int sync_bit; + + if (argc < 3) { + printf("usage: %s InputBitFile OutputModemRawFile\n", argv[0]); + printf("e.g %s hts1a.c2 hts1a_fdmdv.raw\n", argv[0]); + exit(1); + } + + if (strcmp(argv[1], "-") == 0) fin = stdin; + else if ( (fin = fopen(argv[1],"rb")) == NULL ) { + fprintf(stderr, "Error opening input bit file: %s: %s.\n", + argv[1], strerror(errno)); + exit(1); + } + + if (strcmp(argv[2], "-") == 0) fout = stdout; + else if ( (fout = fopen(argv[2],"wb")) == NULL ) { + fprintf(stderr, "Error opening output modem sample file: %s: %s.\n", + argv[2], strerror(errno)); + exit(1); + } + + fdmdv = fdmdv_create(); + frames = 0; + + while(fread(packed_bits, sizeof(char), BYTES_PER_CODEC_FRAME, fin) == BYTES_PER_CODEC_FRAME) { + frames++; + + /* unpack bits, MSB first */ + + bit = 7; byte = 0; + for(i=0; i> bit) & 0x1; + bit--; + if (bit < 0) { + bit = 7; + byte++; + } + } + assert(byte == BYTES_PER_CODEC_FRAME); + + /* modulate even and odd frames */ + + fdmdv_mod(fdmdv, tx_fdm, tx_bits, &sync_bit); + assert(sync_bit == 1); + + fdmdv_mod(fdmdv, &tx_fdm[FDMDV_NOM_SAMPLES_PER_FRAME], &tx_bits[FDMDV_BITS_PER_FRAME], &sync_bit); + assert(sync_bit == 0); + + /* scale and save to disk as shorts */ + + for(i=0; i<2*FDMDV_NOM_SAMPLES_PER_FRAME; i++) + tx_fdm_scaled[i] = FDMDV_SCALE * tx_fdm[i].real; + + fwrite(tx_fdm_scaled, sizeof(short), 2*FDMDV_NOM_SAMPLES_PER_FRAME, fout); + + /* if this is in a pipeline, we probably don't want the usual + buffering to occur */ + + if (fout == stdout) fflush(stdout); + if (fin == stdin) fflush(stdin); + } + + //fdmdv_dump_osc_mags(fdmdv); + + fclose(fin); + fclose(fout); + fdmdv_destroy(fdmdv); + + return 0; +} diff --git a/libs/libcodec2/src/fdmdv_put_test_bits.c b/libs/libcodec2/src/fdmdv_put_test_bits.c new file mode 100644 index 0000000000..ed773e7c59 --- /dev/null +++ b/libs/libcodec2/src/fdmdv_put_test_bits.c @@ -0,0 +1,112 @@ +/*---------------------------------------------------------------------------*\ + + FILE........: fdmdv_put_test_bits.c + AUTHOR......: David Rowe + DATE CREATED: 1 May 2012 + + Using a file of packed test bits as input, determines bit error + rate. Useful for testing fdmdv_demod. + +\*---------------------------------------------------------------------------*/ + + +/* + Copyright (C) 2012 David Rowe + + All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 2, as + published by the Free Software Foundation. This program is + distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program; if not, see . +*/ + +#include +#include +#include +#include +#include +#include + +#include "fdmdv.h" + +#define BITS_PER_CODEC_FRAME (2*FDMDV_BITS_PER_FRAME) +#define BYTES_PER_CODEC_FRAME (BITS_PER_CODEC_FRAME/8) + +int main(int argc, char *argv[]) +{ + FILE *fin; + struct FDMDV *fdmdv; + char packed_bits[BYTES_PER_CODEC_FRAME]; + int rx_bits[2*FDMDV_BITS_PER_FRAME]; + int i, bit, byte; + int test_frame_sync, bit_errors, total_bit_errors, total_bits, ntest_bits; + + if (argc < 2) { + printf("usage: %s InputBitFile\n", argv[0]); + printf("e.g %s test.c2\n", argv[0]); + exit(1); + } + + if (strcmp(argv[1], "-") == 0) fin = stdin; + else if ( (fin = fopen(argv[1],"rb")) == NULL ) { + fprintf(stderr, "Error opening input bit file: %s: %s.\n", + argv[1], strerror(errno)); + exit(1); + } + + fdmdv = fdmdv_create(); + total_bit_errors = 0; + total_bits = 0; + + while(fread(packed_bits, sizeof(char), BYTES_PER_CODEC_FRAME, fin) == BYTES_PER_CODEC_FRAME) { + /* unpack bits, MSB first */ + + bit = 7; byte = 0; + for(i=0; i> bit) & 0x1; + //printf("%d 0x%x %d\n", i, packed_bits[byte], rx_bits[i]); + bit--; + if (bit < 0) { + bit = 7; + byte++; + } + } + assert(byte == BYTES_PER_CODEC_FRAME); + + fdmdv_put_test_bits(fdmdv, &test_frame_sync, &bit_errors, &ntest_bits, rx_bits); + if (test_frame_sync == 1) { + total_bit_errors += bit_errors; + total_bits = total_bits + ntest_bits; + printf("+"); + } + else + printf("-"); + fdmdv_put_test_bits(fdmdv, &test_frame_sync, &bit_errors, &ntest_bits, &rx_bits[FDMDV_BITS_PER_FRAME]); + if (test_frame_sync == 1) { + total_bit_errors += bit_errors; + total_bits = total_bits + ntest_bits; + printf("+"); + } + else + printf("-"); + + /* if this is in a pipeline, we probably don't want the usual + buffering to occur */ + + if (fin == stdin) fflush(stdin); + } + + fclose(fin); + fdmdv_destroy(fdmdv); + + printf("\nbits %d errors %d BER %1.4f\n", total_bits, total_bit_errors, (float)total_bit_errors/(1E-6+total_bits) ); + return 0; +} + diff --git a/libs/libcodec2/src/fifo.c b/libs/libcodec2/src/fifo.c new file mode 100644 index 0000000000..4d224da7e1 --- /dev/null +++ b/libs/libcodec2/src/fifo.c @@ -0,0 +1,143 @@ +/*---------------------------------------------------------------------------*\ + + FILE........: fifo.c + AUTHOR......: David Rowe + DATE CREATED: Oct 15 2012 + + A FIFO design useful in gluing the FDMDV modem and codec together in + integrated applications. The unittest/tfifo indicates these + routines are thread safe without the need for syncronisation + object, e.g. a different thread can read and write to a fifo at the + same time. + +\*---------------------------------------------------------------------------*/ + +/* + Copyright (C) 2012 David Rowe + + All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 2.1, as + published by the Free Software Foundation. This program is + distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program; if not, see . +*/ + +#include +#include +#include +#include "fifo.h" + +struct FIFO { + short *buf; + short *pin; + short *pout; + int nshort; +}; + +struct FIFO *fifo_create(int nshort) { + struct FIFO *fifo; + + fifo = (struct FIFO *)malloc(sizeof(struct FIFO)); + assert(fifo != NULL); + + fifo->buf = (short*)malloc(sizeof(short)*nshort); + assert(fifo->buf != NULL); + fifo->pin = fifo->buf; + fifo->pout = fifo->buf; + fifo->nshort = nshort; + + return fifo; +} + +void fifo_destroy(struct FIFO *fifo) { + assert(fifo != NULL); + free(fifo->buf); + free(fifo); +} + +int fifo_write(struct FIFO *fifo, short data[], int n) { + int i; + int fifo_free; + short *pdata; + short *pin = fifo->pin; + + assert(fifo != NULL); + assert(data != NULL); + + // available storage is one less than nshort as prd == pwr + // is reserved for empty rather than full + + fifo_free = fifo->nshort - fifo_used(fifo) - 1; + + if (n > fifo_free) { + return -1; + } + else { + + /* This could be made more efficient with block copies + using memcpy */ + + pdata = data; + for(i=0; ibuf + fifo->nshort)) + pin = fifo->buf; + } + fifo->pin = pin; + } + + return 0; +} + +int fifo_read(struct FIFO *fifo, short data[], int n) +{ + int i; + short *pdata; + short *pin = fifo->pin; + short *pout = fifo->pout; + + assert(fifo != NULL); + assert(data != NULL); + + if (n > fifo_used(fifo)) { + return -1; + } + else { + + /* This could be made more efficient with block copies + using memcpy */ + + pdata = data; + for(i=0; ibuf + fifo->nshort)) + pout = fifo->buf; + } + fifo->pout = pout; + } + + return 0; +} + +int fifo_used(struct FIFO *fifo) +{ + short *pin = fifo->pin; + short *pout = fifo->pout; + unsigned int used; + + assert(fifo != NULL); + if (pin >= pout) + used = pin - pout; + else + used = fifo->nshort + (unsigned int)(pin - pout); + + return used; +} + diff --git a/libs/libcodec2/src/fifo.h b/libs/libcodec2/src/fifo.h new file mode 100644 index 0000000000..a6a10395de --- /dev/null +++ b/libs/libcodec2/src/fifo.h @@ -0,0 +1,48 @@ +/*---------------------------------------------------------------------------*\ + + FILE........: fifo.h + AUTHOR......: David Rowe + DATE CREATED: Oct 15 2012 + + A FIFO design useful in gluing the FDMDV modem and codec together in + integrated applications. + +\*---------------------------------------------------------------------------*/ + +/* + Copyright (C) 2012 David Rowe + + All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 2.1, as + published by the Free Software Foundation. This program is + distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program; if not, see . +*/ + +#ifndef __FIFO__ +#define __FIFO__ + +#ifdef __cplusplus +extern "C" { +#endif + +struct FIFO; + +struct FIFO *fifo_create(int nshort); +void fifo_destroy(struct FIFO *fifo); +int fifo_write(struct FIFO *fifo, short data[], int n); +int fifo_read(struct FIFO *fifo, short data[], int n); +int fifo_used(struct FIFO *fifo); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/libs/libcodec2/src/generate_codebook.c b/libs/libcodec2/src/generate_codebook.c new file mode 100644 index 0000000000..0bea80d854 --- /dev/null +++ b/libs/libcodec2/src/generate_codebook.c @@ -0,0 +1,179 @@ +/*---------------------------------------------------------------------------*\ + + FILE........: generate_codebook.c + AUTHOR......: Bruce Perens + DATE CREATED: 29 Sep 2010 + + Generate header files containing LSP quantisers, runs at compile time. + +\*---------------------------------------------------------------------------*/ + +/* + All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 2.1, as + published by the Free Software Foundation. This program is + distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program; if not, see . +*/ + +#include +#include +#include +#include + +static const char usage[] = +"Usage: %s filename array_name [filename ...]\n" +"\tCreate C code for codebook tables.\n"; + +static const char format[] = +"The table format must be:\n" +"\tTwo integers describing the dimensions of the codebook.\n" +"\tThen, enough numbers to fill the specified dimensions.\n"; + +static const char header[] = +"/* THIS IS A GENERATED FILE. Edit generate_codebook.c and its input */\n\n" +"/*\n" +" * This intermediary file and the files that used to create it are under \n" +" * The LGPL. See the file COPYING.\n" +" */\n\n" +"#include \"defines.h\"\n\n"; + +struct codebook { + unsigned int k; + unsigned int log2m; + unsigned int m; + float * cb; +}; + +static void +dump_array(const struct codebook * b, int index) +{ + int limit = b->k * b->m; + int i; + + printf("static const float codes%d[] = {\n", index); + for ( i = 0; i < limit; i++ ) { + printf(" %g", b->cb[i]); + if ( i < limit - 1 ) + printf(","); + + /* organise VQs by rows, looks prettier */ + if ( ((i+1) % b->k) == 0 ) + printf("\n"); + } + printf("};\n"); +} + +static void +dump_structure(const struct codebook * b, int index) +{ + printf(" {\n"); + printf(" %d,\n", b->k); + printf(" %g,\n", log(b->m) / log(2)); + printf(" %d,\n", b->m); + printf(" codes%d\n", index); + printf(" }"); +} + +float +get_float(FILE * in, const char * name, char * * cursor, char * buffer, + int size) +{ + for ( ; ; ) { + char * s = *cursor; + char c; + + while ( (c = *s) != '\0' && !isdigit(c) && c != '-' && c != '.' ) + s++; + + /* Comments start with "#" and continue to the end of the line. */ + if ( c != '\0' && c != '#' ) { + char * end = 0; + float f = 0; + + f = strtod(s, &end); + + if ( end != s ) + *cursor = end; + return f; + } + + if ( fgets(buffer, size, in) == NULL ) { + fprintf(stderr, "%s: Format error. %s\n", name, format); + exit(1); + } + *cursor = buffer; + } +} + +static struct codebook * +load(FILE * file, const char * name) +{ + char line[1024]; + char * cursor = line; + struct codebook * b = malloc(sizeof(struct codebook)); + int i; + int size; + + *cursor = '\0'; + + b->k = (int)get_float(file, name, &cursor, line, sizeof(line)); + b->m = (int)get_float(file, name ,&cursor, line, sizeof(line)); + size = b->k * b->m; + + b->cb = (float *)malloc(size * sizeof(float)); + + for ( i = 0; i < size; i++ ) + b->cb[i] = get_float(file, name, &cursor, line, sizeof(line)); + + return b; +} + +int +main(int argc, char * * argv) +{ + struct codebook * * cb = malloc(argc * sizeof(struct codebook *)); + int i; + + if ( argc < 2 ) { + fprintf(stderr, usage, argv[0]); + fprintf(stderr, format); + exit(1); + } + + for ( i = 0; i < argc - 2; i++ ) { + FILE * in = fopen(argv[i + 2], "r"); + + if ( in == NULL ) { + perror(argv[i + 2]); + exit(1); + } + + cb[i] = load(in, argv[i + 2]); + + fclose(in); + } + + printf(header); + for ( i = 0; i < argc - 2; i++ ) { + printf(" /* %s */\n", argv[i + 2]); + dump_array(cb[i], i); + } + printf("\nconst struct lsp_codebook %s[] = {\n", argv[1]); + for ( i = 0; i < argc - 2; i++ ) { + printf(" /* %s */\n", argv[i + 2]); + dump_structure(cb[i], i); + printf(",\n"); + } + printf(" { 0, 0, 0, 0 }\n"); + printf("};\n"); + + return 0; +} diff --git a/libs/libcodec2/src/genlspdtcb.c b/libs/libcodec2/src/genlspdtcb.c new file mode 100644 index 0000000000..efac19c513 --- /dev/null +++ b/libs/libcodec2/src/genlspdtcb.c @@ -0,0 +1,90 @@ +/*---------------------------------------------------------------------------*\ + + FILE........: genlspdtcb.c + AUTHOR......: David Rowe + DATE CREATED: 2 Nov 2011 + + Generates codebooks (quantisation tables) for LSP delta-T VQ. + +\*---------------------------------------------------------------------------*/ + +/* + All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 2.1, as + published by the Free Software Foundation. This program is + distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program; if not, see . + +*/ + +#define MAX_ROWS 10 + +float lsp1to4[] = { + -25,0,25, + -25,0,25, + -50,0,50, + -50,0,50 +}; + +float lsp5to10[] = { + -50,0,50, + -50,0,50, + -50,0,50, + -50,0,50, + -50,0,50, + -50,0,50 +}; + +#include +#include +#include +#include +#include + +void create_codebook_text_file(char filename[], float lsp[], + int rows, int cols); + +int main(void) { + create_codebook_text_file("codebook/lspdt1-4.txt", lsp1to4, 4, 3); + create_codebook_text_file("codebook/lspdt5-10.txt", lsp5to10, 6, 3); + return 0; +} + +void create_codebook_text_file(char filename[], float lsp[], + int rows, int cols) +{ + FILE *f; + int i, digits[MAX_ROWS]; + + f = fopen(filename, "wt"); + if (f == NULL) { + printf("Can't open codebook text file %s\n", filename); + exit(0); + } + + for(i=0; i. */ #include "sine.h" /* global defines for coder */ diff --git a/libs/libcodec2/src/globals.h b/libs/libcodec2/src/globals.h index 44aab8b0a0..cef720344c 100644 --- a/libs/libcodec2/src/globals.h +++ b/libs/libcodec2/src/globals.h @@ -22,8 +22,7 @@ License for more details. You should have received a copy of the GNU Lesser General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program; if not, see . */ /* Globals used in encoder and decoder */ diff --git a/libs/libcodec2/src/glottal.c b/libs/libcodec2/src/glottal.c new file mode 100644 index 0000000000..8ac3ff4a93 --- /dev/null +++ b/libs/libcodec2/src/glottal.c @@ -0,0 +1,257 @@ +const float glottal[]={ + 0.000000, + -0.057687, + -0.115338, + -0.172917, + -0.230385, + -0.287707, + -0.344845, + -0.401762, + -0.458419, + -0.514781, + -0.570809, + -0.626467, + -0.681721, + -0.736537, + -0.790884, + -0.844733, + -0.898057, + -0.950834, + -1.003044, + -1.054670, + -1.105700, + -1.156124, + -1.205936, + -1.255132, + -1.303711, + -1.351675, + -1.399026, + -1.445769, + -1.491908, + -1.537448, + -1.582393, + -1.626747, + -1.670514, + -1.713693, + -1.756285, + -1.798288, + -1.839697, + -1.880507, + -1.920712, + -1.960302, + -1.999269, + -2.037603, + -2.075295, + -2.112335, + -2.148716, + -2.184430, + -2.219472, + -2.253839, + -2.287531, + -2.320550, + -2.352900, + -2.384588, + -2.415623, + -2.446019, + -2.475788, + -2.504946, + -2.533512, + -2.561501, + -2.588934, + -2.615827, + -2.642198, + -2.668064, + -2.693439, + -2.718337, + -2.742767, + -2.766738, + -2.790256, + -2.813322, + -2.835936, + -2.858094, + -2.879790, + -2.901016, + -2.921759, + -2.942008, + -2.961747, + -2.980961, + -2.999632, + -3.017745, + -3.035282, + -3.052228, + -3.068567, + -3.084285, + -3.099371, + -3.113813, + -3.127605, + -3.140738, + 3.129975, + 3.118167, + 3.107022, + 3.096537, + 3.086709, + 3.077531, + 3.068996, + 3.061096, + 3.053821, + 3.047159, + 3.041102, + 3.035636, + 3.030753, + 3.026441, + 3.022690, + 3.019491, + 3.016836, + 3.014718, + 3.013132, + 3.012072, + 3.011535, + 3.011521, + 3.012028, + 3.013057, + 3.014612, + 3.016695, + 3.019310, + 3.022463, + 3.026160, + 3.030407, + 3.035212, + 3.040580, + 3.046520, + 3.053038, + 3.060141, + 3.067836, + 3.076128, + 3.085023, + 3.094525, + 3.104639, + 3.115367, + 3.126712, + 3.138674, + -3.131930, + -3.118731, + -3.104915, + -3.090485, + -3.075444, + -3.059795, + -3.043543, + -3.026695, + -3.009254, + -2.991229, + -2.972625, + -2.953449, + -2.933710, + -2.913414, + -2.892567, + -2.871176, + -2.849248, + -2.826787, + -2.803798, + -2.780284, + -2.756247, + -2.731689, + -2.706609, + -2.681005, + -2.654875, + -2.628213, + -2.601015, + -2.573272, + -2.544977, + -2.516121, + -2.486694, + -2.456686, + -2.426084, + -2.394879, + -2.363060, + -2.330616, + -2.297538, + -2.263816, + -2.229444, + -2.194416, + -2.158727, + -2.122375, + -2.085359, + -2.047682, + -2.009347, + -1.970361, + -1.930732, + -1.890470, + -1.849587, + -1.808098, + -1.766017, + -1.723360, + -1.680145, + -1.636388, + -1.592105, + -1.547313, + -1.502025, + -1.456256, + -1.410016, + -1.363314, + -1.316157, + -1.268547, + -1.220486, + -1.171971, + -1.122997, + -1.073555, + -1.023636, + -0.973227, + -0.922312, + -0.870875, + -0.818899, + -0.766366, + -0.713257, + -0.659554, + -0.605242, + -0.550303, + -0.494723, + -0.438492, + -0.381598, + -0.324036, + -0.265800, + -0.206889, + -0.147303, + -0.087046, + -0.026121, + 0.035463, + 0.097698, + 0.160576, + 0.224087, + 0.288221, + 0.352969, + 0.418323, + 0.484276, + 0.550822, + 0.617958, + 0.685681, + 0.753991, + 0.822889, + 0.892378, + 0.962462, + 1.033144, + 1.104430, + 1.176325, + 1.248833, + 1.321956, + 1.395696, + 1.470051, + 1.545019, + 1.620593, + 1.696763, + 1.773516, + 1.850837, + 1.928705, + 2.007097, + 2.085987, + 2.165347, + 2.245145, + 2.325347, + 2.405919, + 2.486824, + 2.568025, + 2.649485, + 2.731167, + 2.813033, + 2.895045, + 2.977167, + 3.059362}; diff --git a/libs/libcodec2/src/hanning.h b/libs/libcodec2/src/hanning.h new file mode 100644 index 0000000000..81d88dcb35 --- /dev/null +++ b/libs/libcodec2/src/hanning.h @@ -0,0 +1,644 @@ +/* Generated by hanning_file() Octave function */ + +const float hanning[]={ + 0, + 2.4171e-05, + 9.66816e-05, + 0.000217525, + 0.000386689, + 0.000604158, + 0.00086991, + 0.00118392, + 0.00154616, + 0.00195659, + 0.00241517, + 0.00292186, + 0.00347661, + 0.00407937, + 0.00473008, + 0.00542867, + 0.00617507, + 0.00696922, + 0.00781104, + 0.00870045, + 0.00963736, + 0.0106217, + 0.0116533, + 0.0127322, + 0.0138581, + 0.0150311, + 0.0162509, + 0.0175175, + 0.0188308, + 0.0201906, + 0.0215968, + 0.0230492, + 0.0245478, + 0.0260923, + 0.0276826, + 0.0293186, + 0.0310001, + 0.032727, + 0.034499, + 0.036316, + 0.0381779, + 0.0400844, + 0.0420354, + 0.0440307, + 0.04607, + 0.0481533, + 0.0502802, + 0.0524506, + 0.0546643, + 0.056921, + 0.0592206, + 0.0615627, + 0.0639473, + 0.0663741, + 0.0688427, + 0.0713531, + 0.0739048, + 0.0764978, + 0.0791318, + 0.0818064, + 0.0845214, + 0.0872767, + 0.0900718, + 0.0929066, + 0.0957807, + 0.0986939, + 0.101646, + 0.104636, + 0.107665, + 0.110732, + 0.113836, + 0.116978, + 0.120156, + 0.123372, + 0.126624, + 0.129912, + 0.133235, + 0.136594, + 0.139989, + 0.143418, + 0.146881, + 0.150379, + 0.153911, + 0.157476, + 0.161074, + 0.164705, + 0.168368, + 0.172063, + 0.17579, + 0.179549, + 0.183338, + 0.187158, + 0.191008, + 0.194888, + 0.198798, + 0.202737, + 0.206704, + 0.2107, + 0.214724, + 0.218775, + 0.222854, + 0.226959, + 0.231091, + 0.235249, + 0.239432, + 0.243641, + 0.247874, + 0.252132, + 0.256414, + 0.260719, + 0.265047, + 0.269398, + 0.273772, + 0.278167, + 0.282584, + 0.287021, + 0.29148, + 0.295958, + 0.300456, + 0.304974, + 0.30951, + 0.314065, + 0.318638, + 0.323228, + 0.327835, + 0.332459, + 0.3371, + 0.341756, + 0.346427, + 0.351113, + 0.355814, + 0.360528, + 0.365256, + 0.369997, + 0.374751, + 0.379516, + 0.384293, + 0.389082, + 0.393881, + 0.398691, + 0.40351, + 0.408338, + 0.413176, + 0.418022, + 0.422876, + 0.427737, + 0.432605, + 0.43748, + 0.44236, + 0.447247, + 0.452138, + 0.457034, + 0.461935, + 0.466839, + 0.471746, + 0.476655, + 0.481568, + 0.486481, + 0.491397, + 0.496313, + 0.501229, + 0.506145, + 0.511061, + 0.515976, + 0.520889, + 0.5258, + 0.530708, + 0.535614, + 0.540516, + 0.545414, + 0.550308, + 0.555197, + 0.560081, + 0.564958, + 0.56983, + 0.574695, + 0.579552, + 0.584402, + 0.589244, + 0.594077, + 0.598901, + 0.603715, + 0.60852, + 0.613314, + 0.618097, + 0.622868, + 0.627628, + 0.632375, + 0.63711, + 0.641831, + 0.646538, + 0.651232, + 0.655911, + 0.660574, + 0.665222, + 0.669855, + 0.67447, + 0.679069, + 0.683651, + 0.688215, + 0.69276, + 0.697287, + 0.701795, + 0.706284, + 0.710752, + 0.7152, + 0.719627, + 0.724033, + 0.728418, + 0.73278, + 0.73712, + 0.741437, + 0.74573, + 0.75, + 0.754246, + 0.758467, + 0.762663, + 0.766833, + 0.770978, + 0.775097, + 0.779189, + 0.783254, + 0.787291, + 0.791301, + 0.795283, + 0.799236, + 0.80316, + 0.807055, + 0.810921, + 0.814756, + 0.81856, + 0.822334, + 0.826077, + 0.829788, + 0.833468, + 0.837115, + 0.840729, + 0.844311, + 0.847859, + 0.851374, + 0.854855, + 0.858301, + 0.861713, + 0.86509, + 0.868431, + 0.871737, + 0.875007, + 0.87824, + 0.881437, + 0.884598, + 0.887721, + 0.890806, + 0.893854, + 0.896864, + 0.899835, + 0.902768, + 0.905661, + 0.908516, + 0.911331, + 0.914106, + 0.916841, + 0.919536, + 0.92219, + 0.924804, + 0.927376, + 0.929907, + 0.932397, + 0.934845, + 0.93725, + 0.939614, + 0.941935, + 0.944213, + 0.946448, + 0.94864, + 0.950789, + 0.952894, + 0.954955, + 0.956972, + 0.958946, + 0.960874, + 0.962759, + 0.964598, + 0.966393, + 0.968142, + 0.969846, + 0.971505, + 0.973118, + 0.974686, + 0.976207, + 0.977683, + 0.979112, + 0.980495, + 0.981832, + 0.983122, + 0.984365, + 0.985561, + 0.986711, + 0.987813, + 0.988868, + 0.989876, + 0.990837, + 0.99175, + 0.992616, + 0.993434, + 0.994204, + 0.994927, + 0.995601, + 0.996228, + 0.996807, + 0.997337, + 0.99782, + 0.998255, + 0.998641, + 0.998979, + 0.999269, + 0.999511, + 0.999704, + 0.999849, + 0.999946, + 0.999994, + 0.999994, + 0.999946, + 0.999849, + 0.999704, + 0.999511, + 0.999269, + 0.998979, + 0.998641, + 0.998255, + 0.99782, + 0.997337, + 0.996807, + 0.996228, + 0.995601, + 0.994927, + 0.994204, + 0.993434, + 0.992616, + 0.99175, + 0.990837, + 0.989876, + 0.988868, + 0.987813, + 0.986711, + 0.985561, + 0.984365, + 0.983122, + 0.981832, + 0.980495, + 0.979112, + 0.977683, + 0.976207, + 0.974686, + 0.973118, + 0.971505, + 0.969846, + 0.968142, + 0.966393, + 0.964598, + 0.962759, + 0.960874, + 0.958946, + 0.956972, + 0.954955, + 0.952894, + 0.950789, + 0.94864, + 0.946448, + 0.944213, + 0.941935, + 0.939614, + 0.93725, + 0.934845, + 0.932397, + 0.929907, + 0.927376, + 0.924804, + 0.92219, + 0.919536, + 0.916841, + 0.914106, + 0.911331, + 0.908516, + 0.905661, + 0.902768, + 0.899835, + 0.896864, + 0.893854, + 0.890806, + 0.887721, + 0.884598, + 0.881437, + 0.87824, + 0.875007, + 0.871737, + 0.868431, + 0.86509, + 0.861713, + 0.858301, + 0.854855, + 0.851374, + 0.847859, + 0.844311, + 0.840729, + 0.837115, + 0.833468, + 0.829788, + 0.826077, + 0.822334, + 0.81856, + 0.814756, + 0.810921, + 0.807055, + 0.80316, + 0.799236, + 0.795283, + 0.791301, + 0.787291, + 0.783254, + 0.779189, + 0.775097, + 0.770978, + 0.766833, + 0.762663, + 0.758467, + 0.754246, + 0.75, + 0.74573, + 0.741437, + 0.73712, + 0.73278, + 0.728418, + 0.724033, + 0.719627, + 0.7152, + 0.710752, + 0.706284, + 0.701795, + 0.697287, + 0.69276, + 0.688215, + 0.683651, + 0.679069, + 0.67447, + 0.669855, + 0.665222, + 0.660574, + 0.655911, + 0.651232, + 0.646538, + 0.641831, + 0.63711, + 0.632375, + 0.627628, + 0.622868, + 0.618097, + 0.613314, + 0.60852, + 0.603715, + 0.598901, + 0.594077, + 0.589244, + 0.584402, + 0.579552, + 0.574695, + 0.56983, + 0.564958, + 0.560081, + 0.555197, + 0.550308, + 0.545414, + 0.540516, + 0.535614, + 0.530708, + 0.5258, + 0.520889, + 0.515976, + 0.511061, + 0.506145, + 0.501229, + 0.496313, + 0.491397, + 0.486481, + 0.481568, + 0.476655, + 0.471746, + 0.466839, + 0.461935, + 0.457034, + 0.452138, + 0.447247, + 0.44236, + 0.43748, + 0.432605, + 0.427737, + 0.422876, + 0.418022, + 0.413176, + 0.408338, + 0.40351, + 0.398691, + 0.393881, + 0.389082, + 0.384293, + 0.379516, + 0.374751, + 0.369997, + 0.365256, + 0.360528, + 0.355814, + 0.351113, + 0.346427, + 0.341756, + 0.3371, + 0.332459, + 0.327835, + 0.323228, + 0.318638, + 0.314065, + 0.30951, + 0.304974, + 0.300456, + 0.295958, + 0.29148, + 0.287021, + 0.282584, + 0.278167, + 0.273772, + 0.269398, + 0.265047, + 0.260719, + 0.256414, + 0.252132, + 0.247874, + 0.243641, + 0.239432, + 0.235249, + 0.231091, + 0.226959, + 0.222854, + 0.218775, + 0.214724, + 0.2107, + 0.206704, + 0.202737, + 0.198798, + 0.194888, + 0.191008, + 0.187158, + 0.183338, + 0.179549, + 0.17579, + 0.172063, + 0.168368, + 0.164705, + 0.161074, + 0.157476, + 0.153911, + 0.150379, + 0.146881, + 0.143418, + 0.139989, + 0.136594, + 0.133235, + 0.129912, + 0.126624, + 0.123372, + 0.120156, + 0.116978, + 0.113836, + 0.110732, + 0.107665, + 0.104636, + 0.101646, + 0.0986939, + 0.0957807, + 0.0929066, + 0.0900718, + 0.0872767, + 0.0845214, + 0.0818064, + 0.0791318, + 0.0764978, + 0.0739048, + 0.0713531, + 0.0688427, + 0.0663741, + 0.0639473, + 0.0615627, + 0.0592206, + 0.056921, + 0.0546643, + 0.0524506, + 0.0502802, + 0.0481533, + 0.04607, + 0.0440307, + 0.0420354, + 0.0400844, + 0.0381779, + 0.036316, + 0.034499, + 0.032727, + 0.0310001, + 0.0293186, + 0.0276826, + 0.0260923, + 0.0245478, + 0.0230492, + 0.0215968, + 0.0201906, + 0.0188308, + 0.0175175, + 0.0162509, + 0.0150311, + 0.0138581, + 0.0127322, + 0.0116533, + 0.0106217, + 0.00963736, + 0.00870045, + 0.00781104, + 0.00696922, + 0.00617507, + 0.00542867, + 0.00473008, + 0.00407937, + 0.00347661, + 0.00292186, + 0.00241517, + 0.00195659, + 0.00154616, + 0.00118392, + 0.00086991, + 0.000604158, + 0.000386689, + 0.000217525, + 9.66816e-05, + 2.4171e-05, + 0 +}; diff --git a/libs/libcodec2/src/interp.c b/libs/libcodec2/src/interp.c index ff7faacb67..a8d818fa42 100644 --- a/libs/libcodec2/src/interp.c +++ b/libs/libcodec2/src/interp.c @@ -22,16 +22,18 @@ License for more details. You should have received a copy of the GNU Lesser General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program; if not, see . */ #include #include #include +#include #include "defines.h" #include "interp.h" +#include "lsp.h" +#include "quantise.h" float sample_log_amp(MODEL *model, float w); @@ -48,6 +50,12 @@ float sample_log_amp(MODEL *model, float w); This version can interpolate the amplitudes between two frames of different Wo and L. + + This version works by log linear interpolation, but listening tests + showed it creates problems in background noise, e.g. hts2a and mmt1. + When this function is used (--dec mode) bg noise appears to be + amplitude modulated, and gets louder. The interp_lsp() function + below seems to do a better job. \*---------------------------------------------------------------------------*/ @@ -108,15 +116,170 @@ float sample_log_amp(MODEL *model, float w) assert(f <= 1.0); if (m < 1) { - log_amp = f*log10(model->A[1]); + log_amp = f*log10(model->A[1] + 1E-6); } else if ((m+1) > model->L) { - log_amp = (1.0-f)*log10(model->A[model->L]); + log_amp = (1.0-f)*log10(model->A[model->L] + 1E-6); } else { - log_amp = (1.0-f)*log10(model->A[m]) + f*log10(model->A[m+1]); + log_amp = (1.0-f)*log10(model->A[m] + 1E-6) + + f*log10(model->A[m+1] + 1E-6); } return log_amp; } +/*---------------------------------------------------------------------------*\ + + FUNCTION....: interp_lsp() + AUTHOR......: David Rowe + DATE CREATED: 10 Nov 2010 + + Given two frames decribed by model parameters 20ms apart, determines + the model parameters of the 10ms frame between them. Assumes + voicing is available for middle (interpolated) frame. Outputs are + amplitudes and Wo for the interpolated frame. + + This version uses interpolation of LSPs, seems to do a better job + with bg noise. + +\*---------------------------------------------------------------------------*/ + +void interpolate_lsp( + kiss_fft_cfg fft_fwd_cfg, + MODEL *interp, /* interpolated model params */ + MODEL *prev, /* previous frames model params */ + MODEL *next, /* next frames model params */ + float *prev_lsps, /* previous frames LSPs */ + float prev_e, /* previous frames LPC energy */ + float *next_lsps, /* next frames LSPs */ + float next_e, /* next frames LPC energy */ + float *ak_interp, /* interpolated aks for this frame */ + float *lsps_interp/* interpolated lsps for this frame */ +) +{ + int i; + float e; + float snr; + + /* trap corner case where V est is probably wrong */ + + if (interp->voiced && !prev->voiced && !next->voiced) { + interp->voiced = 0; + } + + /* Wo depends on voicing of this and adjacent frames */ + + if (interp->voiced) { + if (prev->voiced && next->voiced) + interp->Wo = (prev->Wo + next->Wo)/2.0; + if (!prev->voiced && next->voiced) + interp->Wo = next->Wo; + if (prev->voiced && !next->voiced) + interp->Wo = prev->Wo; + } + else { + interp->Wo = TWO_PI/P_MAX; + } + interp->L = PI/interp->Wo; + + //printf(" interp: prev_v: %d next_v: %d prev_Wo: %f next_Wo: %f\n", + // prev->voiced, next->voiced, prev->Wo, next->Wo); + //printf(" interp: Wo: %1.5f L: %d\n", interp->Wo, interp->L); + + /* interpolate LSPs */ + + for(i=0; iA[1]); +} + + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: interp_Wo() + AUTHOR......: David Rowe + DATE CREATED: 22 May 2012 + + Interpolates centre 10ms sample of Wo and L samples given two + samples 20ms apart. Assumes voicing is available for centre + (interpolated) frame. + +\*---------------------------------------------------------------------------*/ + +void interp_Wo( + MODEL *interp, /* interpolated model params */ + MODEL *prev, /* previous frames model params */ + MODEL *next /* next frames model params */ + ) +{ + /* trap corner case where voicing est is probably wrong */ + + if (interp->voiced && !prev->voiced && !next->voiced) { + interp->voiced = 0; + } + + /* Wo depends on voicing of this and adjacent frames */ + + if (interp->voiced) { + if (prev->voiced && next->voiced) + interp->Wo = (prev->Wo + next->Wo)/2.0; + if (!prev->voiced && next->voiced) + interp->Wo = next->Wo; + if (prev->voiced && !next->voiced) + interp->Wo = prev->Wo; + } + else { + interp->Wo = TWO_PI/P_MAX; + } + interp->L = PI/interp->Wo; +} + + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: interp_energy() + AUTHOR......: David Rowe + DATE CREATED: 22 May 2012 + + Interpolates centre 10ms sample of energy given two samples 20ms + apart. + +\*---------------------------------------------------------------------------*/ + +float interp_energy(float prev_e, float next_e) +{ + return pow(10.0, (log10(prev_e) + log10(next_e))/2.0); + +} + + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: interpolate_lsp_ver2() + AUTHOR......: David Rowe + DATE CREATED: 22 May 2012 + + Weighted interpolation of LSPs. + +\*---------------------------------------------------------------------------*/ + +void interpolate_lsp_ver2(float interp[], float prev[], float next[], float weight) +{ + int i; + + for(i=0; i. */ #ifndef __INTERP__ #define __INTERP__ +#include "kiss_fft.h" + void interpolate(MODEL *interp, MODEL *prev, MODEL *next); +void interpolate_lsp(kiss_fft_cfg fft_dec_cfg, + MODEL *interp, MODEL *prev, MODEL *next, + float *prev_lsps, float prev_e, + float *next_lsps, float next_e, + float *ak_interp, float *lsps_interp); +void interp_Wo(MODEL *interp, MODEL *prev, MODEL *next); +float interp_energy(float prev, float next); +void interpolate_lsp_ver2(float interp[], float prev[], float next[], float weight); #endif diff --git a/libs/libcodec2/src/kiss_fft.c b/libs/libcodec2/src/kiss_fft.c new file mode 100644 index 0000000000..465d6c97a0 --- /dev/null +++ b/libs/libcodec2/src/kiss_fft.c @@ -0,0 +1,408 @@ +/* +Copyright (c) 2003-2010, Mark Borgerding + +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + + +#include "_kiss_fft_guts.h" +/* The guts header contains all the multiplication and addition macros that are defined for + fixed or floating point complex numbers. It also delares the kf_ internal functions. + */ + +static void kf_bfly2( + kiss_fft_cpx * Fout, + const size_t fstride, + const kiss_fft_cfg st, + int m + ) +{ + kiss_fft_cpx * Fout2; + kiss_fft_cpx * tw1 = st->twiddles; + kiss_fft_cpx t; + Fout2 = Fout + m; + do{ + C_FIXDIV(*Fout,2); C_FIXDIV(*Fout2,2); + + C_MUL (t, *Fout2 , *tw1); + tw1 += fstride; + C_SUB( *Fout2 , *Fout , t ); + C_ADDTO( *Fout , t ); + ++Fout2; + ++Fout; + }while (--m); +} + +static void kf_bfly4( + kiss_fft_cpx * Fout, + const size_t fstride, + const kiss_fft_cfg st, + const size_t m + ) +{ + kiss_fft_cpx *tw1,*tw2,*tw3; + kiss_fft_cpx scratch[6]; + size_t k=m; + const size_t m2=2*m; + const size_t m3=3*m; + + + tw3 = tw2 = tw1 = st->twiddles; + + do { + C_FIXDIV(*Fout,4); C_FIXDIV(Fout[m],4); C_FIXDIV(Fout[m2],4); C_FIXDIV(Fout[m3],4); + + C_MUL(scratch[0],Fout[m] , *tw1 ); + C_MUL(scratch[1],Fout[m2] , *tw2 ); + C_MUL(scratch[2],Fout[m3] , *tw3 ); + + C_SUB( scratch[5] , *Fout, scratch[1] ); + C_ADDTO(*Fout, scratch[1]); + C_ADD( scratch[3] , scratch[0] , scratch[2] ); + C_SUB( scratch[4] , scratch[0] , scratch[2] ); + C_SUB( Fout[m2], *Fout, scratch[3] ); + tw1 += fstride; + tw2 += fstride*2; + tw3 += fstride*3; + C_ADDTO( *Fout , scratch[3] ); + + if(st->inverse) { + Fout[m].r = scratch[5].r - scratch[4].i; + Fout[m].i = scratch[5].i + scratch[4].r; + Fout[m3].r = scratch[5].r + scratch[4].i; + Fout[m3].i = scratch[5].i - scratch[4].r; + }else{ + Fout[m].r = scratch[5].r + scratch[4].i; + Fout[m].i = scratch[5].i - scratch[4].r; + Fout[m3].r = scratch[5].r - scratch[4].i; + Fout[m3].i = scratch[5].i + scratch[4].r; + } + ++Fout; + }while(--k); +} + +static void kf_bfly3( + kiss_fft_cpx * Fout, + const size_t fstride, + const kiss_fft_cfg st, + size_t m + ) +{ + size_t k=m; + const size_t m2 = 2*m; + kiss_fft_cpx *tw1,*tw2; + kiss_fft_cpx scratch[5]; + kiss_fft_cpx epi3; + epi3 = st->twiddles[fstride*m]; + + tw1=tw2=st->twiddles; + + do{ + C_FIXDIV(*Fout,3); C_FIXDIV(Fout[m],3); C_FIXDIV(Fout[m2],3); + + C_MUL(scratch[1],Fout[m] , *tw1); + C_MUL(scratch[2],Fout[m2] , *tw2); + + C_ADD(scratch[3],scratch[1],scratch[2]); + C_SUB(scratch[0],scratch[1],scratch[2]); + tw1 += fstride; + tw2 += fstride*2; + + Fout[m].r = Fout->r - HALF_OF(scratch[3].r); + Fout[m].i = Fout->i - HALF_OF(scratch[3].i); + + C_MULBYSCALAR( scratch[0] , epi3.i ); + + C_ADDTO(*Fout,scratch[3]); + + Fout[m2].r = Fout[m].r + scratch[0].i; + Fout[m2].i = Fout[m].i - scratch[0].r; + + Fout[m].r -= scratch[0].i; + Fout[m].i += scratch[0].r; + + ++Fout; + }while(--k); +} + +static void kf_bfly5( + kiss_fft_cpx * Fout, + const size_t fstride, + const kiss_fft_cfg st, + int m + ) +{ + kiss_fft_cpx *Fout0,*Fout1,*Fout2,*Fout3,*Fout4; + int u; + kiss_fft_cpx scratch[13]; + kiss_fft_cpx * twiddles = st->twiddles; + kiss_fft_cpx *tw; + kiss_fft_cpx ya,yb; + ya = twiddles[fstride*m]; + yb = twiddles[fstride*2*m]; + + Fout0=Fout; + Fout1=Fout0+m; + Fout2=Fout0+2*m; + Fout3=Fout0+3*m; + Fout4=Fout0+4*m; + + tw=st->twiddles; + for ( u=0; ur += scratch[7].r + scratch[8].r; + Fout0->i += scratch[7].i + scratch[8].i; + + scratch[5].r = scratch[0].r + S_MUL(scratch[7].r,ya.r) + S_MUL(scratch[8].r,yb.r); + scratch[5].i = scratch[0].i + S_MUL(scratch[7].i,ya.r) + S_MUL(scratch[8].i,yb.r); + + scratch[6].r = S_MUL(scratch[10].i,ya.i) + S_MUL(scratch[9].i,yb.i); + scratch[6].i = -S_MUL(scratch[10].r,ya.i) - S_MUL(scratch[9].r,yb.i); + + C_SUB(*Fout1,scratch[5],scratch[6]); + C_ADD(*Fout4,scratch[5],scratch[6]); + + scratch[11].r = scratch[0].r + S_MUL(scratch[7].r,yb.r) + S_MUL(scratch[8].r,ya.r); + scratch[11].i = scratch[0].i + S_MUL(scratch[7].i,yb.r) + S_MUL(scratch[8].i,ya.r); + scratch[12].r = - S_MUL(scratch[10].i,yb.i) + S_MUL(scratch[9].i,ya.i); + scratch[12].i = S_MUL(scratch[10].r,yb.i) - S_MUL(scratch[9].r,ya.i); + + C_ADD(*Fout2,scratch[11],scratch[12]); + C_SUB(*Fout3,scratch[11],scratch[12]); + + ++Fout0;++Fout1;++Fout2;++Fout3;++Fout4; + } +} + +/* perform the butterfly for one stage of a mixed radix FFT */ +static void kf_bfly_generic( + kiss_fft_cpx * Fout, + const size_t fstride, + const kiss_fft_cfg st, + int m, + int p + ) +{ + int u,k,q1,q; + kiss_fft_cpx * twiddles = st->twiddles; + kiss_fft_cpx t; + int Norig = st->nfft; + + kiss_fft_cpx * scratch = (kiss_fft_cpx*)KISS_FFT_TMP_ALLOC(sizeof(kiss_fft_cpx)*p); + + for ( u=0; u=Norig) twidx-=Norig; + C_MUL(t,scratch[q] , twiddles[twidx] ); + C_ADDTO( Fout[ k ] ,t); + } + k += m; + } + } + KISS_FFT_TMP_FREE(scratch); +} + +static +void kf_work( + kiss_fft_cpx * Fout, + const kiss_fft_cpx * f, + const size_t fstride, + int in_stride, + int * factors, + const kiss_fft_cfg st + ) +{ + kiss_fft_cpx * Fout_beg=Fout; + const int p=*factors++; /* the radix */ + const int m=*factors++; /* stage's fft length/p */ + const kiss_fft_cpx * Fout_end = Fout + p*m; + +#ifdef _OPENMP + // use openmp extensions at the + // top-level (not recursive) + if (fstride==1 && p<=5) + { + int k; + + // execute the p different work units in different threads +# pragma omp parallel for + for (k=0;k floor_sqrt) + p = n; /* no more factors, skip to end */ + } + n /= p; + *facbuf++ = p; + *facbuf++ = n; + } while (n > 1); +} + +/* + * + * User-callable function to allocate all necessary storage space for the fft. + * + * The return value is a contiguous block of memory, allocated with malloc. As such, + * It can be freed with free(), rather than a kiss_fft-specific function. + * */ +kiss_fft_cfg kiss_fft_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem ) +{ + kiss_fft_cfg st=NULL; + size_t memneeded = sizeof(struct kiss_fft_state) + + sizeof(kiss_fft_cpx)*(nfft-1); /* twiddle factors*/ + + if ( lenmem==NULL ) { + st = ( kiss_fft_cfg)KISS_FFT_MALLOC( memneeded ); + }else{ + if (mem != NULL && *lenmem >= memneeded) + st = (kiss_fft_cfg)mem; + *lenmem = memneeded; + } + if (st) { + int i; + st->nfft=nfft; + st->inverse = inverse_fft; + + for (i=0;iinverse) + phase *= -1; + kf_cexp(st->twiddles+i, phase ); + } + + kf_factor(nfft,st->factors); + } + return st; +} + + +void kiss_fft_stride(kiss_fft_cfg st,const kiss_fft_cpx *fin,kiss_fft_cpx *fout,int in_stride) +{ + if (fin == fout) { + //NOTE: this is not really an in-place FFT algorithm. + //It just performs an out-of-place FFT into a temp buffer + kiss_fft_cpx * tmpbuf = (kiss_fft_cpx*)KISS_FFT_TMP_ALLOC( sizeof(kiss_fft_cpx)*st->nfft); + kf_work(tmpbuf,fin,1,in_stride, st->factors,st); + memcpy(fout,tmpbuf,sizeof(kiss_fft_cpx)*st->nfft); + KISS_FFT_TMP_FREE(tmpbuf); + }else{ + kf_work( fout, fin, 1,in_stride, st->factors,st ); + } +} + +void kiss_fft(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout) +{ + kiss_fft_stride(cfg,fin,fout,1); +} + + +void kiss_fft_cleanup(void) +{ + // nothing needed any more +} + +int kiss_fft_next_fast_size(int n) +{ + while(1) { + int m=n; + while ( (m%2) == 0 ) m/=2; + while ( (m%3) == 0 ) m/=3; + while ( (m%5) == 0 ) m/=5; + if (m<=1) + break; /* n is completely factorable by twos, threes, and fives */ + n++; + } + return n; +} diff --git a/libs/libcodec2/src/kiss_fft.h b/libs/libcodec2/src/kiss_fft.h new file mode 100644 index 0000000000..64c50f4aae --- /dev/null +++ b/libs/libcodec2/src/kiss_fft.h @@ -0,0 +1,124 @@ +#ifndef KISS_FFT_H +#define KISS_FFT_H + +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* + ATTENTION! + If you would like a : + -- a utility that will handle the caching of fft objects + -- real-only (no imaginary time component ) FFT + -- a multi-dimensional FFT + -- a command-line utility to perform ffts + -- a command-line utility to perform fast-convolution filtering + + Then see kfc.h kiss_fftr.h kiss_fftnd.h fftutil.c kiss_fastfir.c + in the tools/ directory. +*/ + +#ifdef USE_SIMD +# include +# define kiss_fft_scalar __m128 +#define KISS_FFT_MALLOC(nbytes) _mm_malloc(nbytes,16) +#define KISS_FFT_FREE _mm_free +#else +#define KISS_FFT_MALLOC malloc +#define KISS_FFT_FREE free +#endif + + +#ifdef FIXED_POINT +#include +# if (FIXED_POINT == 32) +# define kiss_fft_scalar int32_t +# else +# define kiss_fft_scalar int16_t +# endif +#else +# ifndef kiss_fft_scalar +/* default is float */ +# define kiss_fft_scalar float +# endif +#endif + +typedef struct { + kiss_fft_scalar r; + kiss_fft_scalar i; +}kiss_fft_cpx; + +typedef struct kiss_fft_state* kiss_fft_cfg; + +/* + * kiss_fft_alloc + * + * Initialize a FFT (or IFFT) algorithm's cfg/state buffer. + * + * typical usage: kiss_fft_cfg mycfg=kiss_fft_alloc(1024,0,NULL,NULL); + * + * The return value from fft_alloc is a cfg buffer used internally + * by the fft routine or NULL. + * + * If lenmem is NULL, then kiss_fft_alloc will allocate a cfg buffer using malloc. + * The returned value should be free()d when done to avoid memory leaks. + * + * The state can be placed in a user supplied buffer 'mem': + * If lenmem is not NULL and mem is not NULL and *lenmem is large enough, + * then the function places the cfg in mem and the size used in *lenmem + * and returns mem. + * + * If lenmem is not NULL and ( mem is NULL or *lenmem is not large enough), + * then the function returns NULL and places the minimum cfg + * buffer size in *lenmem. + * */ + +kiss_fft_cfg kiss_fft_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem); + +/* + * kiss_fft(cfg,in_out_buf) + * + * Perform an FFT on a complex input buffer. + * for a forward FFT, + * fin should be f[0] , f[1] , ... ,f[nfft-1] + * fout will be F[0] , F[1] , ... ,F[nfft-1] + * Note that each element is complex and can be accessed like + f[k].r and f[k].i + * */ +void kiss_fft(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout); + +/* + A more generic version of the above function. It reads its input from every Nth sample. + * */ +void kiss_fft_stride(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout,int fin_stride); + +/* If kiss_fft_alloc allocated a buffer, it is one contiguous + buffer and can be simply free()d when no longer needed*/ +#define kiss_fft_free free + +/* + Cleans up some memory that gets managed internally. Not necessary to call, but it might clean up + your compiler output to call this before you exit. +*/ +void kiss_fft_cleanup(void); + + +/* + * Returns the smallest integer k, such that k>=n and k has only "fast" factors (2,3,5) + */ +int kiss_fft_next_fast_size(int n); + +/* for real ffts, we need an even size */ +#define kiss_fftr_next_fast_size_real(n) \ + (kiss_fft_next_fast_size( ((n)+1)>>1)<<1) + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/libs/libcodec2/src/listensim.sh b/libs/libcodec2/src/listensim.sh index 64f7455ab3..b296cac588 100755 --- a/libs/libcodec2/src/listensim.sh +++ b/libs/libcodec2/src/listensim.sh @@ -4,6 +4,6 @@ # # Listen to files processed with sim.sh -../script/menu.sh ../raw/$1.raw $1_uq.raw $1_phase0.raw $1_lpc10.raw $1_lsp.raw $1_phase0_lpc10.raw $1_phase0_lsp.raw $1_phase0_lsp_dec.raw $2 $3 +../script/menu.sh $1_uq.raw $1_lpc10.raw $1_lpcpf.raw $1_phase0.raw $1_phase0_lpcpf.raw $2 $3 $4 $5 diff --git a/libs/libcodec2/src/lpc.c b/libs/libcodec2/src/lpc.c index 1f9ff2bf10..a253289a46 100644 --- a/libs/libcodec2/src/lpc.c +++ b/libs/libcodec2/src/lpc.c @@ -2,14 +2,14 @@ FILE........: lpc.c AUTHOR......: David Rowe - DATE CREATED: 30/9/90 + DATE CREATED: 30 Sep 1990 (!) Linear Prediction functions written in C. \*---------------------------------------------------------------------------*/ /* - Copyright (C) 2009 David Rowe + Copyright (C) 2009-2012 David Rowe All rights reserved. @@ -22,18 +22,74 @@ License for more details. You should have received a copy of the GNU Lesser General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program; if not, see . */ #define LPC_MAX_N 512 /* maximum no. of samples in frame */ #define PI 3.141592654 /* mathematical constant */ +#define ALPHA 1.0 +#define BETA 0.94 + #include #include #include "defines.h" #include "lpc.h" +/*---------------------------------------------------------------------------*\ + + pre_emp() + + Pre-emphasise (high pass filter with zero close to 0 Hz) a frame of + speech samples. Helps reduce dynamic range of LPC spectrum, giving + greater weight and hensea better match to low energy formants. + + Should be balanced by de-emphasis of the output speech. + +\*---------------------------------------------------------------------------*/ + +void pre_emp( + float Sn_pre[], /* output frame of speech samples */ + float Sn[], /* input frame of speech samples */ + float *mem, /* Sn[-1]single sample memory */ + int Nsam /* number of speech samples to use */ +) +{ + int i; + + for(i=0; i. */ #ifndef __LPC__ @@ -31,6 +30,8 @@ #define LPC_MAX_ORDER 20 +void pre_emp(float Sn_pre[], float Sn[], float *mem, int Nsam); +void de_emp(float Sn_se[], float Sn[], float *mem, int Nsam); void hanning_window(float Sn[], float Wn[], int Nsam); void autocorrelate(float Sn[], float Rn[], int Nsam, int order); void levinson_durbin(float R[], float lpcs[], int order); diff --git a/libs/libcodec2/src/lsp.c b/libs/libcodec2/src/lsp.c index feab4219ab..47001c1efd 100644 --- a/libs/libcodec2/src/lsp.c +++ b/libs/libcodec2/src/lsp.c @@ -1,323 +1,325 @@ -/*---------------------------------------------------------------------------*\ - - FILE........: lsp.c - AUTHOR......: David Rowe - DATE CREATED: 24/2/93 - - - This file contains functions for LPC to LSP conversion and LSP to - LPC conversion. Note that the LSP coefficients are not in radians - format but in the x domain of the unit circle. - -\*---------------------------------------------------------------------------*/ - -#include "defines.h" -#include "lsp.h" -#include -#include -#include - -/*---------------------------------------------------------------------------*\ - - Introduction to Line Spectrum Pairs (LSPs) - ------------------------------------------ - - LSPs are used to encode the LPC filter coefficients {ak} for - transmission over the channel. LSPs have several properties (like - less sensitivity to quantisation noise) that make them superior to - direct quantisation of {ak}. - - A(z) is a polynomial of order lpcrdr with {ak} as the coefficients. - - A(z) is transformed to P(z) and Q(z) (using a substitution and some - algebra), to obtain something like: - - A(z) = 0.5[P(z)(z+z^-1) + Q(z)(z-z^-1)] (1) - - As you can imagine A(z) has complex zeros all over the z-plane. P(z) - and Q(z) have the very neat property of only having zeros _on_ the - unit circle. So to find them we take a test point z=exp(jw) and - evaluate P (exp(jw)) and Q(exp(jw)) using a grid of points between 0 - and pi. - - The zeros (roots) of P(z) also happen to alternate, which is why we - swap coefficients as we find roots. So the process of finding the - LSP frequencies is basically finding the roots of 5th order - polynomials. - - The root so P(z) and Q(z) occur in symmetrical pairs at +/-w, hence - the name Line Spectrum Pairs (LSPs). - - To convert back to ak we just evaluate (1), "clocking" an impulse - thru it lpcrdr times gives us the impulse response of A(z) which is - {ak}. - -\*---------------------------------------------------------------------------*/ - -/*---------------------------------------------------------------------------*\ - - FUNCTION....: cheb_poly_eva() - AUTHOR......: David Rowe - DATE CREATED: 24/2/93 - - This function evalutes a series of chebyshev polynomials - - FIXME: performing memory allocation at run time is very inefficient, - replace with stack variables of MAX_P size. - -\*---------------------------------------------------------------------------*/ - - -float cheb_poly_eva(float *coef,float x,int m) -/* float coef[] coefficients of the polynomial to be evaluated */ -/* float x the point where polynomial is to be evaluated */ -/* int m order of the polynomial */ -{ - int i; - float *T,*t,*u,*v,sum; - - /* Allocate memory for chebyshev series formulation */ - - if((T = (float *)malloc((m/2+1)*sizeof(float))) == NULL){ - fprintf(stderr, "not enough memory to allocate buffer\n"); - exit(1); - } - - /* Initialise pointers */ - - t = T; /* T[i-2] */ - *t++ = 1.0; - u = t--; /* T[i-1] */ - *u++ = x; - v = u--; /* T[i] */ - - /* Evaluate chebyshev series formulation using iterative approach */ - - for(i=2;i<=m/2;i++) - *v++ = (2*x)*(*u++) - *t++; /* T[i] = 2*x*T[i-1] - T[i-2] */ - - sum=0.0; /* initialise sum to zero */ - t = T; /* reset pointer */ - - /* Evaluate polynomial and return value also free memory space */ - - for(i=0;i<=m/2;i++) - sum+=coef[(m/2)-i]**t++; - - free(T); - return sum; -} - - -/*---------------------------------------------------------------------------*\ - - FUNCTION....: lpc_to_lsp() - AUTHOR......: David Rowe - DATE CREATED: 24/2/93 - - This function converts LPC coefficients to LSP coefficients. - -\*---------------------------------------------------------------------------*/ - -int lpc_to_lsp (float *a, int lpcrdr, float *freq, int nb, float delta) -/* float *a lpc coefficients */ -/* int lpcrdr order of LPC coefficients (10) */ -/* float *freq LSP frequencies in radians */ -/* int nb number of sub-intervals (4) */ -/* float delta grid spacing interval (0.02) */ -{ - float psuml,psumr,psumm,temp_xr,xl,xr,xm; - float temp_psumr; - int i,j,m,flag,k; - float *Q; /* ptrs for memory allocation */ - float *P; - float *px; /* ptrs of respective P'(z) & Q'(z) */ - float *qx; - float *p; - float *q; - float *pt; /* ptr used for cheb_poly_eval() - whether P' or Q' */ - int roots=0; /* number of roots found */ - flag = 1; - m = lpcrdr/2; /* order of P'(z) & Q'(z) polynimials */ - - /* Allocate memory space for polynomials */ - - Q = (float *) malloc((m+1)*sizeof(float)); - P = (float *) malloc((m+1)*sizeof(float)); - if( (P == NULL) || (Q == NULL) ) { - fprintf(stderr,"not enough memory to allocate buffer\n"); - exit(1); - } - - /* determine P'(z)'s and Q'(z)'s coefficients where - P'(z) = P(z)/(1 + z^(-1)) and Q'(z) = Q(z)/(1-z^(-1)) */ - - px = P; /* initilaise ptrs */ - qx = Q; - p = px; - q = qx; - *px++ = 1.0; - *qx++ = 1.0; - for(i=1;i<=m;i++){ - *px++ = a[i]+a[lpcrdr+1-i]-*p++; - *qx++ = a[i]-a[lpcrdr+1-i]+*q++; - } - px = P; - qx = Q; - for(i=0;i= -1.0)){ - xr = xl - delta ; /* interval spacing */ - psumr = cheb_poly_eva(pt,xr,lpcrdr);/* poly(xl-delta_x) */ - temp_psumr = psumr; - temp_xr = xr; - - /* if no sign change increment xr and re-evaluate - poly(xr). Repeat til sign change. if a sign change has - occurred the interval is bisected and then checked again - for a sign change which determines in which interval the - zero lies in. If there is no sign change between poly(xm) - and poly(xl) set interval between xm and xr else set - interval between xl and xr and repeat till root is located - within the specified limits */ - - if((psumr*psuml)<0.0){ - roots++; - - psumm=psuml; - for(k=0;k<=nb;k++){ - xm = (xl+xr)/2; /* bisect the interval */ - psumm=cheb_poly_eva(pt,xm,lpcrdr); - if(psumm*psuml>0.){ - psuml=psumm; - xl=xm; - } - else{ - psumr=psumm; - xr=xm; - } - } - - /* once zero is found, reset initial interval to xr */ - freq[j] = (xm); - xl = xm; - flag = 0; /* reset flag for next search */ - } - else{ - psuml=temp_psumr; - xl=temp_xr; - } - } - } - free(P); /* free memory space */ - free(Q); - - /* convert from x domain to radians */ - - for(i=0; i. +*/ + +#include "defines.h" +#include "lsp.h" +#include +#include +#include + +/* Only 10 gets used, so far. */ +#define LSP_MAX_ORDER 20 + +/*---------------------------------------------------------------------------*\ + + Introduction to Line Spectrum Pairs (LSPs) + ------------------------------------------ + + LSPs are used to encode the LPC filter coefficients {ak} for + transmission over the channel. LSPs have several properties (like + less sensitivity to quantisation noise) that make them superior to + direct quantisation of {ak}. + + A(z) is a polynomial of order lpcrdr with {ak} as the coefficients. + + A(z) is transformed to P(z) and Q(z) (using a substitution and some + algebra), to obtain something like: + + A(z) = 0.5[P(z)(z+z^-1) + Q(z)(z-z^-1)] (1) + + As you can imagine A(z) has complex zeros all over the z-plane. P(z) + and Q(z) have the very neat property of only having zeros _on_ the + unit circle. So to find them we take a test point z=exp(jw) and + evaluate P (exp(jw)) and Q(exp(jw)) using a grid of points between 0 + and pi. + + The zeros (roots) of P(z) also happen to alternate, which is why we + swap coefficients as we find roots. So the process of finding the + LSP frequencies is basically finding the roots of 5th order + polynomials. + + The root so P(z) and Q(z) occur in symmetrical pairs at +/-w, hence + the name Line Spectrum Pairs (LSPs). + + To convert back to ak we just evaluate (1), "clocking" an impulse + thru it lpcrdr times gives us the impulse response of A(z) which is + {ak}. + +\*---------------------------------------------------------------------------*/ + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: cheb_poly_eva() + AUTHOR......: David Rowe + DATE CREATED: 24/2/93 + + This function evalutes a series of chebyshev polynomials + + FIXME: performing memory allocation at run time is very inefficient, + replace with stack variables of MAX_P size. + +\*---------------------------------------------------------------------------*/ + + +static float +cheb_poly_eva(float *coef,float x,int m) +/* float coef[] coefficients of the polynomial to be evaluated */ +/* float x the point where polynomial is to be evaluated */ +/* int m order of the polynomial */ +{ + int i; + float *t,*u,*v,sum; + float T[(LSP_MAX_ORDER / 2) + 1]; + + /* Initialise pointers */ + + t = T; /* T[i-2] */ + *t++ = 1.0; + u = t--; /* T[i-1] */ + *u++ = x; + v = u--; /* T[i] */ + + /* Evaluate chebyshev series formulation using iterative approach */ + + for(i=2;i<=m/2;i++) + *v++ = (2*x)*(*u++) - *t++; /* T[i] = 2*x*T[i-1] - T[i-2] */ + + sum=0.0; /* initialise sum to zero */ + t = T; /* reset pointer */ + + /* Evaluate polynomial and return value also free memory space */ + + for(i=0;i<=m/2;i++) + sum+=coef[(m/2)-i]**t++; + + return sum; +} + + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: lpc_to_lsp() + AUTHOR......: David Rowe + DATE CREATED: 24/2/93 + + This function converts LPC coefficients to LSP coefficients. + +\*---------------------------------------------------------------------------*/ + +int lpc_to_lsp (float *a, int lpcrdr, float *freq, int nb, float delta) +/* float *a lpc coefficients */ +/* int lpcrdr order of LPC coefficients (10) */ +/* float *freq LSP frequencies in radians */ +/* int nb number of sub-intervals (4) */ +/* float delta grid spacing interval (0.02) */ +{ + float psuml,psumr,psumm,temp_xr,xl,xr,xm = 0; + float temp_psumr; + int i,j,m,flag,k; + float *px; /* ptrs of respective P'(z) & Q'(z) */ + float *qx; + float *p; + float *q; + float *pt; /* ptr used for cheb_poly_eval() + whether P' or Q' */ + int roots=0; /* number of roots found */ + float Q[LSP_MAX_ORDER + 1]; + float P[LSP_MAX_ORDER + 1]; + + flag = 1; + m = lpcrdr/2; /* order of P'(z) & Q'(z) polynimials */ + + /* Allocate memory space for polynomials */ + + /* determine P'(z)'s and Q'(z)'s coefficients where + P'(z) = P(z)/(1 + z^(-1)) and Q'(z) = Q(z)/(1-z^(-1)) */ + + px = P; /* initilaise ptrs */ + qx = Q; + p = px; + q = qx; + *px++ = 1.0; + *qx++ = 1.0; + for(i=1;i<=m;i++){ + *px++ = a[i]+a[lpcrdr+1-i]-*p++; + *qx++ = a[i]-a[lpcrdr+1-i]+*q++; + } + px = P; + qx = Q; + for(i=0;i= -1.0)){ + xr = xl - delta ; /* interval spacing */ + psumr = cheb_poly_eva(pt,xr,lpcrdr);/* poly(xl-delta_x) */ + temp_psumr = psumr; + temp_xr = xr; + + /* if no sign change increment xr and re-evaluate + poly(xr). Repeat til sign change. if a sign change has + occurred the interval is bisected and then checked again + for a sign change which determines in which interval the + zero lies in. If there is no sign change between poly(xm) + and poly(xl) set interval between xm and xr else set + interval between xl and xr and repeat till root is located + within the specified limits */ + + if((psumr*psuml)<0.0){ + roots++; + + psumm=psuml; + for(k=0;k<=nb;k++){ + xm = (xl+xr)/2; /* bisect the interval */ + psumm=cheb_poly_eva(pt,xm,lpcrdr); + if(psumm*psuml>0.){ + psuml=psumm; + xl=xm; + } + else{ + psumr=psumm; + xr=xm; + } + } + + /* once zero is found, reset initial interval to xr */ + freq[j] = (xm); + xl = xm; + flag = 0; /* reset flag for next search */ + } + else{ + psuml=temp_psumr; + xl=temp_xr; + } + } + } + + /* convert from x domain to radians */ + + for(i=0; i. +*/ + #ifndef __LSP__ #define __LSP__ diff --git a/libs/libcodec2/src/nlp.c b/libs/libcodec2/src/nlp.c index 193ca92109..3214578e84 100644 --- a/libs/libcodec2/src/nlp.c +++ b/libs/libcodec2/src/nlp.c @@ -4,8 +4,8 @@ AUTHOR......: David Rowe DATE CREATED: 23/3/93 - Non Linear Pitch (NLP) estimation functions. - + Non Linear Pitch (NLP) estimation functions. + \*---------------------------------------------------------------------------*/ /* @@ -22,14 +22,13 @@ License for more details. You should have received a copy of the GNU Lesser General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program; if not, see . */ #include "defines.h" #include "nlp.h" #include "dump.h" -#include "four1.h" +#include "kiss_fft.h" #include #include @@ -60,7 +59,7 @@ /* 48 tap 600Hz low pass FIR filter coefficients */ -float nlp_fir[] = { +const float nlp_fir[] = { -1.0818124e-03, -1.1008344e-03, -9.2768838e-04, @@ -112,12 +111,14 @@ float nlp_fir[] = { }; typedef struct { - float sq[PMAX_M]; /* squared speech samples */ - float mem_x,mem_y; /* memory for notch filter */ - float mem_fir[NLP_NTAP]; /* decimation FIR filter memory */ + float sq[PMAX_M]; /* squared speech samples */ + float mem_x,mem_y; /* memory for notch filter */ + float mem_fir[NLP_NTAP]; /* decimation FIR filter memory */ + kiss_fft_cfg fft_cfg; /* kiss FFT config */ } NLP; -float post_process_mbe(COMP Fw[], int pmin, int pmax, float gmax); +float test_candidate_mbe(COMP Sw[], COMP W[], float f0); +float post_process_mbe(COMP Fw[], int pmin, int pmax, float gmax, COMP Sw[], COMP W[], float *prev_Wo); float post_process_sub_multiples(COMP Fw[], int pmin, int pmax, float gmax, int gmax_bin, float *prev_Wo); @@ -146,20 +147,27 @@ void *nlp_create() for(i=0; imem_fir[i] = 0.0; + nlp->fft_cfg = kiss_fft_alloc (PE_FFT_SIZE, 0, NULL, NULL); + assert(nlp->fft_cfg != NULL); + return (void*)nlp; } /*---------------------------------------------------------------------------*\ - nlp_destory() + nlp_destroy() - Initialisation function for NLP pitch estimator. + Shut down function for NLP pitch estimator. \*---------------------------------------------------------------------------*/ void nlp_destroy(void *nlp_state) { + NLP *nlp; assert(nlp_state != NULL); + nlp = (NLP*)nlp_state; + + KISS_FFT_FREE(nlp->fft_cfg); free(nlp_state); } @@ -198,27 +206,30 @@ float nlp( float Sn[], /* input speech vector */ int n, /* frames shift (no. new samples in Sn[]) */ int m, /* analysis window size */ - int pmin, /* minimum pitch value */ + int pmin, /* minimum pitch value */ int pmax, /* maximum pitch value */ float *pitch, /* estimated pitch period in samples */ COMP Sw[], /* Freq domain version of Sn[] */ + COMP W[], /* Freq domain window */ float *prev_Wo ) { NLP *nlp; - float notch; /* current notch filter output */ - COMP Fw[PE_FFT_SIZE]; /* DFT of squared signal */ + float notch; /* current notch filter output */ + COMP fw[PE_FFT_SIZE]; /* DFT of squared signal (input) */ + COMP Fw[PE_FFT_SIZE]; /* DFT of squared signal (output) */ float gmax; int gmax_bin; int i,j; float best_f0; assert(nlp_state != NULL); + assert(m <= PMAX_M); nlp = (NLP*)nlp_state; /* Square, notch filter at DC, and LP filter vector */ - for(i=m-n; isq[i] = Sn[i]*Sn[i]; for(i=m-n; imem_y; nlp->mem_x = nlp->sq[i]; nlp->mem_y = notch; - nlp->sq[i] = notch; + nlp->sq[i] = notch + 1.0; /* With 0 input vectors to codec, + kiss_fft() would take a long + time to execute when running in + real time. Problem was traced + to kiss_fft function call in + this function. Adding this small + constant fixed problem. Not + exactly sure why. */ } for(i=m-n; isq[i*DEC]*(0.5 - 0.5*cos(2*PI*i/(m/DEC-1))); + fw[i].real = nlp->sq[i*DEC]*(0.5 - 0.5*cos(2*PI*i/(m/DEC-1))); } + #ifdef DUMP dump_dec(Fw); - four1(&Fw[-1].imag,PE_FFT_SIZE,1); + #endif + + kiss_fft(nlp->fft_cfg, (kiss_fft_cpx *)fw, (kiss_fft_cpx *)Fw); for(i=0; isq); dump_Fw(Fw); + #endif /* find global peak */ @@ -267,9 +290,13 @@ float nlp( gmax_bin = i; } } - - best_f0 = post_process_sub_multiples(Fw, pmin, pmax, gmax, gmax_bin, - prev_Wo); + + //#define POST_PROCESS_MBE + #ifdef POST_PROCESS_MBE + best_f0 = post_process_mbe(Fw, pmin, pmax, gmax, Sw, W, prev_Wo); + #else + best_f0 = post_process_sub_multiples(Fw, pmin, pmax, gmax, gmax_bin, prev_Wo); + #endif /* Shift samples in buffer to make room for new samples */ @@ -286,7 +313,7 @@ float nlp( post_process_sub_multiples() - Given the global maximma of Fw[] we search interger submultiples for + Given the global maximma of Fw[] we search integer submultiples for local maxima. If local maxima exist and they are above an experimentally derived threshold (OK a magic number I pulled out of the air) we choose the submultiple as the F0 estimate. @@ -317,10 +344,10 @@ float post_process_sub_multiples(COMP Fw[], /* post process estimate by searching submultiples */ mult = 2; - min_bin = PE_FFT_SIZE*DEC/pmax; + min_bin = PE_FFT_SIZE*DEC/pmax; cmax_bin = gmax_bin; prev_f0_bin = *prev_Wo*(4000.0/PI)*(PE_FFT_SIZE*DEC)/SAMPLE_RATE; - + while(gmax_bin/mult >= min_bin) { b = gmax_bin/mult; /* determine search interval */ @@ -339,7 +366,7 @@ float post_process_sub_multiples(COMP Fw[], lmax = 0; lmax_bin = bmin; - for (b=bmin; b<=bmax; b++) /* look for maximum in interval */ + for (b=bmin; b<=bmax; b++) /* look for maximum in interval */ if (Fw[b].real > lmax) { lmax = Fw[b].real; lmax_bin = b; @@ -359,3 +386,158 @@ float post_process_sub_multiples(COMP Fw[], return best_f0; } +/*---------------------------------------------------------------------------*\ + + post_process_mbe() + + Use the MBE pitch estimation algorithm to evaluate pitch candidates. This + works OK but the accuracy at low F0 is affected by NW, the analysis window + size used for the DFT of the input speech Sw[]. Also favours high F0 in + the presence of background noise which causes periodic artifacts in the + synthesised speech. + +\*---------------------------------------------------------------------------*/ + +float post_process_mbe(COMP Fw[], int pmin, int pmax, float gmax, COMP Sw[], COMP W[], float *prev_Wo) +{ + float candidate_f0; + float f0,best_f0; /* fundamental frequency */ + float e,e_min; /* MBE cost function */ + int i; + float e_hz[F0_MAX]; + int bin; + float f0_min, f0_max; + float f0_start, f0_end; + + f0_min = (float)SAMPLE_RATE/pmax; + f0_max = (float)SAMPLE_RATE/pmin; + + /* Now look for local maxima. Each local maxima is a candidate + that we test using the MBE pitch estimation algotithm */ + + for(i=0; i Fw[i-1].real) && (Fw[i].real > Fw[i+1].real)) { + + /* local maxima found, lets test if it's big enough */ + + if (Fw[i].real > T*gmax) { + + /* OK, sample MBE cost function over +/- 10Hz range in 2.5Hz steps */ + + candidate_f0 = (float)i*SAMPLE_RATE/(PE_FFT_SIZE*DEC); + f0_start = candidate_f0-20; + f0_end = candidate_f0+20; + if (f0_start < f0_min) f0_start = f0_min; + if (f0_end > f0_max) f0_end = f0_max; + + for(f0=f0_start; f0<=f0_end; f0+= 2.5) { + e = test_candidate_mbe(Sw, W, f0); + bin = floor(f0); assert((bin > 0) && (bin < F0_MAX)); + e_hz[bin] = e; + if (e < e_min) { + e_min = e; + best_f0 = f0; + } + } + + } + } + } + + /* finally sample MBE cost function around previous pitch estimate + (form of pitch tracking) */ + + candidate_f0 = *prev_Wo * SAMPLE_RATE/TWO_PI; + f0_start = candidate_f0-20; + f0_end = candidate_f0+20; + if (f0_start < f0_min) f0_start = f0_min; + if (f0_end > f0_max) f0_end = f0_max; + + for(f0=f0_start; f0<=f0_end; f0+= 2.5) { + e = test_candidate_mbe(Sw, W, f0); + bin = floor(f0); assert((bin > 0) && (bin < F0_MAX)); + e_hz[bin] = e; + if (e < e_min) { + e_min = e; + best_f0 = f0; + } + } + + #ifdef DUMP + dump_e(e_hz); + #endif + + return best_f0; +} + +/*---------------------------------------------------------------------------*\ + + test_candidate_mbe() + + Returns the error of the MBE cost function for the input f0. + + Note: I think a lot of the operations below can be simplified as + W[].imag = 0 and has been normalised such that den always equals 1. + +\*---------------------------------------------------------------------------*/ + +float test_candidate_mbe( + COMP Sw[], + COMP W[], + float f0 +) +{ + COMP Sw_[FFT_ENC]; /* DFT of all voiced synthesised signal */ + int l,al,bl,m; /* loop variables */ + COMP Am; /* amplitude sample for this band */ + int offset; /* centers Hw[] about current harmonic */ + float den; /* denominator of Am expression */ + float error; /* accumulated error between originl and synthesised */ + float Wo; /* current "test" fundamental freq. */ + int L; + + L = floor((SAMPLE_RATE/2.0)/f0); + Wo = f0*(2*PI/SAMPLE_RATE); + + error = 0.0; + + /* Just test across the harmonics in the first 1000 Hz (L/4) */ + + for(l=1; l. */ #ifndef __NLP__ #define __NLP__ +#include "comp.h" + void *nlp_create(); void nlp_destroy(void *nlp_state); float nlp(void *nlp_state, float Sn[], int n, int m, int pmin, int pmax, - float *pitch, COMP Sw[], float *prev_Wo); -float test_candidate_mbe(COMP Sw[], float f0, COMP Sw_[]); + float *pitch, COMP Sw[], COMP W[], float *prev_Wo); #endif diff --git a/libs/libcodec2/src/octave.c b/libs/libcodec2/src/octave.c new file mode 100644 index 0000000000..2ff5ad1413 --- /dev/null +++ b/libs/libcodec2/src/octave.c @@ -0,0 +1,85 @@ +/*---------------------------------------------------------------------------*\ + + FILE........: octave.c + AUTHOR......: David Rowe + DATE CREATED: April 28 2012 + + Functions to save C arrays in GNU Octave matrix format. The output text + file can be directly read into Octave using "load filename". + +\*---------------------------------------------------------------------------*/ + + +/* + Copyright (C) 2012 David Rowe + + All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 2, as + published by the Free Software Foundation. This program is + distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program; if not, see . +*/ + +#include +#include "octave.h" + +void octave_save_int(FILE *f, char name[], int data[], int rows, int cols) +{ + int r,c; + + fprintf(f, "# name: %s\n", name); + fprintf(f, "# type: matrix\n"); + fprintf(f, "# rows: %d\n", rows); + fprintf(f, "# columns: %d\n", cols); + + for(r=0; r. +*/ + +#ifndef __OCTAVE__ +#define __OCTAVE__ + +#include "comp.h" + +void octave_save_int(FILE *f, char name[], int data[], int rows, int cols); +void octave_save_float(FILE *f, char name[], float data[], int rows, int cols, int col_len); +void octave_save_complex(FILE *f, char name[], COMP data[], int rows, int cols, int col_len); + +#endif diff --git a/libs/libcodec2/src/os.h b/libs/libcodec2/src/os.h new file mode 100644 index 0000000000..0dae9bfd24 --- /dev/null +++ b/libs/libcodec2/src/os.h @@ -0,0 +1,53 @@ +/* Generate using fir1(47,1/6) in Octave */ + +const float fdmdv_os_filter[]= { + -3.55606818e-04, + -8.98615286e-04, + -1.40119781e-03, + -1.71713852e-03, + -1.56471179e-03, + -6.28128960e-04, + 1.24522223e-03, + 3.83138676e-03, + 6.41309478e-03, + 7.85893186e-03, + 6.93514929e-03, + 2.79361991e-03, + -4.51051400e-03, + -1.36671853e-02, + -2.21034939e-02, + -2.64084653e-02, + -2.31425052e-02, + -9.84218694e-03, + 1.40648474e-02, + 4.67316298e-02, + 8.39615986e-02, + 1.19925275e-01, + 1.48381174e-01, + 1.64097819e-01, + 1.64097819e-01, + 1.48381174e-01, + 1.19925275e-01, + 8.39615986e-02, + 4.67316298e-02, + 1.40648474e-02, + -9.84218694e-03, + -2.31425052e-02, + -2.64084653e-02, + -2.21034939e-02, + -1.36671853e-02, + -4.51051400e-03, + 2.79361991e-03, + 6.93514929e-03, + 7.85893186e-03, + 6.41309478e-03, + 3.83138676e-03, + 1.24522223e-03, + -6.28128960e-04, + -1.56471179e-03, + -1.71713852e-03, + -1.40119781e-03, + -8.98615286e-04, + -3.55606818e-04 +}; + diff --git a/libs/libcodec2/src/pack.c b/libs/libcodec2/src/pack.c index 2cbff4438a..5d67c3296e 100644 --- a/libs/libcodec2/src/pack.c +++ b/libs/libcodec2/src/pack.c @@ -1,20 +1,20 @@ /* Copyright (C) 2010 Perens LLC - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. + All rights reserved. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 2.1, as + published by the Free Software Foundation. This program is + distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. - You should have received a copy of the GNU General Public License - along with this program. If not, see . + You should have received a copy of the GNU Lesser General Public License + along with this program; if not, see . +*/ - */ #include "defines.h" #include "quantise.h" #include @@ -81,7 +81,8 @@ unpack( unsigned int fieldWidth/* Width of the field in BITS, not bytes. */ ) { - unsigned int field = 0; + unsigned int field = 0; + unsigned int t; do { unsigned int bI = *bitIndex; @@ -96,7 +97,7 @@ unpack( } while ( fieldWidth != 0 ); /* Convert from Gray code to binary. Works for maximum 8-bit fields. */ - unsigned int t = field ^ (field >> 8); + t = field ^ (field >> 8); t ^= (t >> 4); t ^= (t >> 2); t ^= (t >> 1); diff --git a/libs/libcodec2/src/phase.c b/libs/libcodec2/src/phase.c index 83fd680e79..d41303b73c 100644 --- a/libs/libcodec2/src/phase.c +++ b/libs/libcodec2/src/phase.c @@ -22,20 +22,22 @@ License for more details. You should have received a copy of the GNU Lesser General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program; if not,see . */ #include "defines.h" #include "phase.h" -#include "four1.h" +#include "kiss_fft.h" +#include "comp.h" +#include "glottal.c" #include +#include #include #include #include -#define VTHRESH 4.0 +#define GLOTTAL_FFT_SIZE 512 /*---------------------------------------------------------------------------*\ @@ -47,6 +49,7 @@ \*---------------------------------------------------------------------------*/ void aks_to_H( + kiss_fft_cfg fft_fwd_cfg, MODEL *model, /* model parameters */ float aks[], /* LPC's */ float G, /* energy term */ @@ -54,7 +57,8 @@ void aks_to_H( int order ) { - COMP Pw[FFT_DEC]; /* power spectrum */ + COMP pw[FFT_ENC]; /* power spectrum (input) */ + COMP Pw[FFT_ENC]; /* power spectrum (output) */ int i,m; /* loop variables */ int am,bm; /* limits of current band */ float r; /* no. rads/bin */ @@ -63,19 +67,19 @@ void aks_to_H( int b; /* centre bin of harmonic */ float phi_; /* phase of LPC spectra */ - r = TWO_PI/(FFT_DEC); + r = TWO_PI/(FFT_ENC); /* Determine DFT of A(exp(jw)) ------------------------------------------*/ - for(i=0; iWo)*N/2; + ex_phase[0] += (*prev_Wo+model->Wo)*N/2; */ ex_phase[0] += (model->Wo)*N; ex_phase[0] -= TWO_PI*floor(ex_phase[0]/TWO_PI + 0.5); + r = TWO_PI/GLOTTAL_FFT_SIZE; for(m=1; m<=model->L; m++) { - + /* generate excitation */ - + if (model->voiced) { - /* This method of adding jitter really helped remove the clicky - sound in low pitched makes like hts1a. This moves the onset - of each harmonic over at +/- 0.25 of a sample. + //float rnd; + + b = floor(m*model->Wo/r + 0.5); + if (b > ((GLOTTAL_FFT_SIZE/2)-1)) { + b = (GLOTTAL_FFT_SIZE/2)-1; + } + + /* I think adding a little jitter helps improve low pitch + males like hts1a. This moves the onset of each harmonic + over +/- 0.25 of a sample. */ - jitter = 0.25*(1.0 - 2.0*rand()/RAND_MAX); - Ex[m].real = cos(ex_phase[0]*m - jitter*model->Wo*m); - Ex[m].imag = sin(ex_phase[0]*m - jitter*model->Wo*m); + //jitter = 0.25*(1.0 - 2.0*rand()/RAND_MAX); + jitter = 0; + + //rnd = (PI/8)*(1.0 - 2.0*rand()/RAND_MAX); + Ex[m].real = cos(ex_phase[0]*m/* - jitter*model->Wo*m + glottal[b]*/); + Ex[m].imag = sin(ex_phase[0]*m/* - jitter*model->Wo*m + glottal[b]*/); } else { @@ -252,3 +270,4 @@ void phase_synth_zero_order( } } + diff --git a/libs/libcodec2/src/phase.h b/libs/libcodec2/src/phase.h index 6dbf3fa2d6..367948dffb 100644 --- a/libs/libcodec2/src/phase.h +++ b/libs/libcodec2/src/phase.h @@ -22,13 +22,18 @@ License for more details. You should have received a copy of the GNU Lesser General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program; if not, see . */ #ifndef __PHASE__ #define __PHASE__ -void phase_synth_zero_order(MODEL *model, float aks[], float *ex_phase); +#include "kiss_fft.h" + +void phase_synth_zero_order(kiss_fft_cfg fft_dec_cfg, + MODEL *model, + float aks[], + float *ex_phase, + int order); #endif diff --git a/libs/libcodec2/src/phaseexp.c b/libs/libcodec2/src/phaseexp.c new file mode 100644 index 0000000000..57db0f0919 --- /dev/null +++ b/libs/libcodec2/src/phaseexp.c @@ -0,0 +1,1574 @@ +/*---------------------------------------------------------------------------*\ + + FILE........: phaseexp.c + AUTHOR......: David Rowe + DATE CREATED: June 2012 + + Experimental functions for quantising, modelling and synthesising phase. + +\*---------------------------------------------------------------------------*/ + +/* + Copyright (C) 2012 David Rowe + + All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 2.1, as + published by the Free Software Foundation. This program is + distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program; if not,see . +*/ + +#include "defines.h" +#include "phase.h" +#include "kiss_fft.h" +#include "comp.h" + +#include +#include +#include +#include +#include + +/* Bruce Perens' funcs to load codebook files */ + +struct codebook { + unsigned int k; + unsigned int log2m; + unsigned int m; + COMP *cb; + unsigned int offset; +}; + +static const char format[] = +"The table format must be:\n" +"\tTwo integers describing the dimensions of the codebook.\n" +"\tThen, enough numbers to fill the specified dimensions.\n"; + +float get_float(FILE * in, const char * name, char * * cursor, char * buffer, int size) +{ + for ( ; ; ) { + char * s = *cursor; + char c; + + while ( (c = *s) != '\0' && !isdigit(c) && c != '-' && c != '.' ) + s++; + + /* Comments start with "#" and continue to the end of the line. */ + if ( c != '\0' && c != '#' ) { + char * end = 0; + float f = 0; + + f = strtod(s, &end); + + if ( end != s ) + *cursor = end; + return f; + } + + if ( fgets(buffer, size, in) == NULL ) { + fprintf(stderr, "%s: Format error. %s\n", name, format); + exit(1); + } + *cursor = buffer; + } +} + +static struct codebook *load(const char * name) +{ + FILE *file; + char line[2048]; + char *cursor = line; + struct codebook *b = malloc(sizeof(struct codebook)); + int i; + int size; + float angle; + + file = fopen(name, "rt"); + assert(file != NULL); + + *cursor = '\0'; + + b->k = (int)get_float(file, name, &cursor, line, sizeof(line)); + b->m = (int)get_float(file, name ,&cursor, line, sizeof(line)); + size = b->k * b->m; + + b->cb = (COMP *)malloc(size * sizeof(COMP)); + + for ( i = 0; i < size; i++ ) { + angle = get_float(file, name, &cursor, line, sizeof(line)); + b->cb[i].real = cos(angle); + b->cb[i].imag = sin(angle); + } + + fclose(file); + + return b; +} + + +/* states for phase experiments */ + +struct PEXP { + float phi1; + float phi_prev[MAX_AMP]; + float Wo_prev; + int frames; + float snr; + float var; + int var_n; + struct codebook *vq1,*vq2,*vq3,*vq4,*vq5; + float vq_var; + int vq_var_n; + MODEL prev_model; + int state; +}; + + +/*---------------------------------------------------------------------------* \ + + phase_experiment_create() + + Inits states for phase quantisation experiments. + +\*---------------------------------------------------------------------------*/ + +struct PEXP * phase_experiment_create() { + struct PEXP *pexp; + int i; + + pexp = (struct PEXP *)malloc(sizeof(struct PEXP)); + assert (pexp != NULL); + + pexp->phi1 = 0; + for(i=0; iphi_prev[i] = 0.0; + pexp->Wo_prev = 0.0; + pexp->frames = 0; + pexp->snr = 0.0; + pexp->var = 0.0; + pexp->var_n = 0; + + /* smoothed 10th order for 1st 1 khz */ + //pexp->vq1 = load("../unittest/ph1_10_1024.txt"); + //pexp->vq1->offset = 0; + + /* load experimental phase VQ */ + + //pexp->vq1 = load("../unittest/testn1_20_1024.txt"); + pexp->vq1 = load("../unittest/test.txt"); + //pexp->vq2 = load("../unittest/testn21_40_1024.txt"); + pexp->vq2 = load("../unittest/test11_20_1024.txt"); + pexp->vq3 = load("../unittest/test21_30_1024.txt"); + pexp->vq4 = load("../unittest/test31_40_1024.txt"); + pexp->vq5 = load("../unittest/test41_60_1024.txt"); + pexp->vq1->offset = 0; + pexp->vq2->offset = 10; + pexp->vq3->offset = 20; + pexp->vq4->offset = 30; + pexp->vq5->offset = 40; + + pexp->vq_var = 0.0; + pexp->vq_var_n = 0; + + pexp->state = 0; + + return pexp; +} + + +/*---------------------------------------------------------------------------* \ + + phase_experiment_destroy() + +\*---------------------------------------------------------------------------*/ + +void phase_experiment_destroy(struct PEXP *pexp) { + assert(pexp != NULL); + if (pexp->snr != 0.0) + printf("snr: %4.2f dB\n", pexp->snr/pexp->frames); + if (pexp->var != 0.0) + printf("var...: %4.3f std dev...: %4.3f (%d non zero phases)\n", + pexp->var/pexp->var_n, sqrt(pexp->var/pexp->var_n), pexp->var_n); + if (pexp->vq_var != 0.0) + printf("vq var: %4.3f vq std dev: %4.3f (%d non zero phases)\n", + pexp->vq_var/pexp->vq_var_n, sqrt(pexp->vq_var/pexp->vq_var_n), pexp->vq_var_n); + free(pexp); +} + + +/*---------------------------------------------------------------------------* \ + + Various test and experimental functions ................ + +\*---------------------------------------------------------------------------*/ + +/* Bubblesort to find highest amplitude harmonics */ + +struct AMPINDEX { + float amp; + int index; +}; + +static void bubbleSort(struct AMPINDEX numbers[], int array_size) +{ + int i, j; + struct AMPINDEX temp; + + for (i = (array_size - 1); i > 0; i--) + { + for (j = 1; j <= i; j++) + { + //printf("i %d j %d %f %f \n", i, j, numbers[j-1].amp, numbers[j].amp); + if (numbers[j-1].amp < numbers[j].amp) + { + temp = numbers[j-1]; + numbers[j-1] = numbers[j]; + numbers[j] = temp; + } + } + } +} + + +static void print_pred_error(struct PEXP *pexp, MODEL *model, int start, int end, float mag_thresh) { + int i; + float mag; + + mag = 0.0; + for(i=start; i<=end; i++) + mag += model->A[i]*model->A[i]; + mag = 10*log10(mag/(end-start)); + + if (mag > mag_thresh) { + for(i=start; i<=end; i++) { + float pred = pexp->phi_prev[i] + N*i*(model->Wo + pexp->Wo_prev)/2.0; + float err = pred - model->phi[i]; + err = atan2(sin(err),cos(err)); + printf("%f\n",err); + } + //printf("\n"); + } + +} + + +static void predict_phases(struct PEXP *pexp, MODEL *model, int start, int end) { + int i; + + for(i=start; i<=end; i++) { + model->phi[i] = pexp->phi_prev[i] + N*i*model->Wo; + } + +} +static float refine_Wo(struct PEXP *pexp, + MODEL *model, + int start, + int end); + +/* Fancy state based phase prediction. Actually works OK on most utterances, + but could use some tuning. Breaks down a bit on mmt1. */ + +static void predict_phases_state(struct PEXP *pexp, MODEL *model, int start, int end) { + int i, next_state; + float best_Wo, dWo; + + //best_Wo = refine_Wo(pexp, model, start, end); + //best_Wo = (model->Wo + pexp->Wo_prev)/2.0; + best_Wo = model->Wo; + + dWo = fabs(model->Wo - pexp->Wo_prev)/model->Wo; + next_state = pexp->state; + switch(pexp->state) { + case 0: + if (dWo < 0.1) { + /* UV -> V transition, so start with phases in lock. They will + drift a bit over voiced track which is kinda what we want, so + we don't get clicky speech. + */ + next_state = 1; + for(i=start; i<=end; i++) + pexp->phi_prev[i] = i*pexp->phi1; + } + + break; + case 1: + if (dWo > 0.1) + next_state = 0; + break; + } + pexp->state = next_state; + + if (pexp->state == 0) + for(i=start; i<=end; i++) { + model->phi[i] = PI*(1.0 - 2.0*rand()/RAND_MAX); + } + else + for(i=start; i<=end; i++) { + model->phi[i] = pexp->phi_prev[i] + N*i*best_Wo; + } + printf("state %d\n", pexp->state); +} + +static void struct_phases(struct PEXP *pexp, MODEL *model, int start, int end) { + int i; + + for(i=start; i<=end; i++) + model->phi[i] = pexp->phi1*i; + +} + + +static void predict_phases2(struct PEXP *pexp, MODEL *model, int start, int end) { + int i; + float pred, str, diff; + + for(i=start; i<=end; i++) { + pred = pexp->phi_prev[i] + N*i*model->Wo; + str = pexp->phi1*i; + diff = str - pred; + diff = atan2(sin(diff), cos(diff)); + if (diff > 0) + pred += PI/16; + else + pred -= PI/16; + model->phi[i] = pred; + } + +} + +static void skip_phases(struct PEXP *pexp, MODEL *model, int start, int end) { + int i; + + for(i=start; i<=end; i+=2) + model->phi[i] = model->phi[i-1] - model->phi[i-2]; + +} + +static void rand_phases(MODEL *model, int start, int end) { + int i; + + for(i=start; i<=end; i++) + model->phi[i] = PI*(1.0 - 2.0*(float)rand()/RAND_MAX); + +} + +static void quant_phase(float *phase, float min, float max, int bits) { + int levels = 1 << bits; + int index; + float norm, step; + + norm = (*phase - min)/(max - min); + index = floor(levels*norm); + + //printf("phase %f norm %f index %d ", *phase, norm, index); + if (index < 0 ) index = 0; + if (index > (levels-1)) index = levels-1; + //printf("index %d ", index); + step = (max - min)/levels; + *phase = min + step*index + 0.5*step; + //printf("step %f phase %f\n", step, *phase); +} + +static void quant_phases(MODEL *model, int start, int end, int bits) { + int i; + + for(i=start; i<=end; i++) { + quant_phase(&model->phi[i], -PI, PI, bits); + } +} + +static void fixed_bits_per_frame(struct PEXP *pexp, MODEL *model, int m, int budget) { + int res, finished; + + res = 3; + finished = 0; + + while(!finished) { + if (m > model->L/2) + res = 2; + if (((budget - res) < 0) || (m > model->L)) + finished = 1; + else { + quant_phase(&model->phi[m], -PI, PI, res); + budget -= res; + m++; + } + } + printf("m: %d L: %d budget: %d\n", m, model->L, budget); + predict_phases(pexp, model, m, model->L); + //rand_phases(model, m, model->L); +} + +/* used to plot histogram of quantisation error, for 3 bits, 8 levels, + should be uniform between +/- PI/8 */ + +static void check_phase_quant(MODEL *model, float tol) +{ + int m; + float phi_before[MAX_AMP]; + + for(m=1; m<=model->L; m++) + phi_before[m] = model->phi[m]; + + quant_phases(model, 1, model->L, 3); + + for(m=1; m<=model->L; m++) { + float err = phi_before[m] - model->phi[m]; + printf("%f\n", err); + if (fabs(err) > tol) + exit(0); + } +} + + +static float est_phi1(MODEL *model, int start, int end) +{ + int m; + float delta, s, c, phi1_est; + + if (end > model->L) + end = model->L; + + s = c = 0.0; + for(m=start; mphi[m+1] - model->phi[m]; + s += sin(delta); + c += cos(delta); + } + + phi1_est = atan2(s,c); + + return phi1_est; +} + +static void print_phi1_pred_error(MODEL *model, int start, int end) +{ + int m; + float phi1_est; + + phi1_est = est_phi1(model, start, end); + + for(m=start; mphi[m+1] - model->phi[m] - phi1_est; + err = atan2(sin(err),cos(err)); + printf("%f\n", err); + } +} + + +static void first_order_band(MODEL *model, int start, int end, float phi1_est) +{ + int m; + float pred_err, av_pred_err; + float c,s; + + s = c = 0.0; + for(m=start; mphi[m] - phi1_est*m; + s += sin(pred_err); + c += cos(pred_err); + } + + av_pred_err = atan2(s,c); + for(m=start; mphi[m] = av_pred_err + phi1_est*m; + model->phi[m] = atan2(sin(model->phi[m]), cos(model->phi[m])); + } + +} + + +static void sub_linear(MODEL *model, int start, int end, float phi1_est) +{ + int m; + + for(m=start; mphi[m] = m*phi1_est; + } +} + + +static void top_amp(struct PEXP *pexp, MODEL *model, int start, int end, int n_harm, int pred) +{ + int removed = 0, not_removed = 0; + int top, i, j; + struct AMPINDEX sorted[MAX_AMP]; + + /* sort into ascending order of amplitude */ + + printf("\n"); + for(i=start,j=0; iA[i]; + sorted[j].index = i; + printf("%f ", model->A[i]); + } + bubbleSort(sorted, end-start); + + printf("\n"); + for(j=0; jA[i] == sorted[j].amp) { + top = 1; + assert(i == sorted[j].index); + } + } + + #define ALTTOP + #ifdef ALTTOP + model->phi[i] = 0.0; /* make sure */ + if (top) { + model->phi[i] = i*pexp->phi1; + removed++; + } + else { + model->phi[i] = PI*(1.0 - 2.0*(float)rand()/RAND_MAX); // note: try rand for higher harms + removed++; + } + #else + if (!top) { + model->phi[i] = 0.0; /* make sure */ + if (pred) { + //model->phi[i] = pexp->phi_prev[i] + i*N*(model->Wo + pexp->Wo_prev)/2.0; + model->phi[i] = i*model->phi[1]; + } + else + model->phi[i] = PI*(1.0 - 2.0*(float)rand()/RAND_MAX); // note: try rand for higher harms + removed++; + } + else { + /* need to make this work thru budget of bits */ + quant_phase(&model->phi[i], -PI, PI, 3); + not_removed++; + } + #endif + } + printf("dim: %d rem %d not_rem %d\n", end-start, removed, not_removed); + +} + + +static void limit_prediction_error(struct PEXP *pexp, MODEL *model, int start, int end, float limit) +{ + int i; + float pred, pred_error, error; + + for(i=start; i<=end; i++) { + pred = pexp->phi_prev[i] + N*i*(model->Wo + pexp->Wo_prev)/2.0; + pred_error = pred - model->phi[i]; + pred_error -= TWO_PI*floor((pred_error+PI)/TWO_PI); + quant_phase(&pred_error, -limit, limit, 2); + + error = pred - pred_error - model->phi[i]; + error -= TWO_PI*floor((error+PI)/TWO_PI); + printf("%f\n", pred_error); + model->phi[i] = pred - pred_error; + } +} + + +static void quant_prediction_error(struct PEXP *pexp, MODEL *model, int start, int end, float limit) +{ + int i; + float pred, pred_error; + + for(i=start; i<=end; i++) { + pred = pexp->phi_prev[i] + N*i*(model->Wo + pexp->Wo_prev)/2.0; + pred_error = pred - model->phi[i]; + pred_error -= TWO_PI*floor((pred_error+PI)/TWO_PI); + + printf("%f\n", pred_error); + model->phi[i] = pred - pred_error; + } +} + + +static void print_sparse_pred_error(struct PEXP *pexp, MODEL *model, int start, int end, float mag_thresh) +{ + int i, index; + float mag, pred, error; + float sparse_pe[MAX_AMP]; + + mag = 0.0; + for(i=start; i<=end; i++) + mag += model->A[i]*model->A[i]; + mag = 10*log10(mag/(end-start)); + + if (mag > mag_thresh) { + for(i=0; iphi_prev[i] + N*i*(model->Wo + pexp->Wo_prev)/2.0; + error = pred - model->phi[i]; + error = atan2(sin(error),cos(error)); + + index = MAX_AMP*i*model->Wo/PI; + assert(index < MAX_AMP); + sparse_pe[index] = error; + } + + /* dump spare phase vector in polar format */ + + for(i=0; iL; m++) { + signal += model->A[m]*model->A[m]; + diff = cos(model->phi[m]) - cos(before[m]); + noise += pow(model->A[m]*diff, 2.0); + diff = sin(model->phi[m]) - sin(before[m]); + noise += pow(model->A[m]*diff, 2.0); + //printf("%f %f\n", before[m], model->phi[m]); + } + //printf("%f %f snr = %f\n", signal, noise, 10.0*log10(signal/noise)); + pexp->snr += 10.0*log10(signal/noise); +} + + +static void update_variance_calc(struct PEXP *pexp, MODEL *model, float before[]) +{ + int m; + float diff; + + for(m=1; mL; m++) { + diff = model->phi[m] - before[m]; + diff = atan2(sin(diff), cos(diff)); + pexp->var += diff*diff; + } + pexp->var_n += model->L; +} + +void print_vec(COMP cb[], int d, int e) +{ + int i,j; + + for(j=0; jWo + pexp->Wo_prev)/2.0; + best_var = 1E32; + for(Wo=0.97*Wo_est; Wo<=1.03*Wo_est; Wo+=0.001*Wo_est) { + + /* predict phase and sum differences between harmonics */ + + var = 0.0; + for(i=start; i<=end; i++) { + pred = pexp->phi_prev[i] + N*i*Wo; + error = pred - model->phi[i]; + error = atan2(sin(error),cos(error)); + var += error*error; + } + + if (var < best_var) { + best_var = var; + best_Wo = Wo; + } + } + + return best_Wo; +} + + +static void split_vq(COMP sparse_pe_out[], struct PEXP *pexp, struct codebook *vq, float weights[], COMP sparse_pe_in[]) +{ + int i, j, non_zero, vq_ind; + + //printf("\n offset %d k %d m %d j: ", vq->offset, vq->k, vq->m); + vq_ind = vq_phase(vq->cb, &sparse_pe_in[vq->offset], &weights[vq->offset], vq->k, vq->m, &pexp->vq_var); + + non_zero = 0; + for(i=0, j=vq->offset; ik; i++,j++) { + //printf("%f ", atan2(sparse_pe[i].imag, sparse_pe[i].real)); + if ((sparse_pe_in[j].real != 0.0) && (sparse_pe_in[j].imag != 0.0)) { + //printf("%d ", j); + sparse_pe_out[j] = vq->cb[vq->k * vq_ind + i]; + non_zero++; + } + } + pexp->vq_var_n += non_zero; +} + + +static void sparse_vq_pred_error(struct PEXP *pexp, + MODEL *model +) +{ + int i, index; + float pred, error, error_q_angle, best_Wo; + COMP sparse_pe_in[MAX_AMP], sparse_pe_out[MAX_AMP]; + float weights[MAX_AMP]; + COMP error_q_rect; + + best_Wo = refine_Wo(pexp, model, 1, model->L); + //best_Wo = (model->Wo + pexp->Wo_prev)/2.0; + + /* transform to sparse pred error vector */ + + for(i=0; iL; i++) { + pred = pexp->phi_prev[i] + N*i*best_Wo; + error = pred - model->phi[i]; + + index = MAX_AMP*i*model->Wo/PI; + assert(index < MAX_AMP); + sparse_pe_in[index].real = cos(error); + sparse_pe_in[index].imag = sin(error); + sparse_pe_out[index] = sparse_pe_in[index]; + weights[index] = model->A[i]; + //printf("%d ", index); + } + + /* vector quantise */ + + split_vq(sparse_pe_out, pexp, pexp->vq1, weights, sparse_pe_in); + split_vq(sparse_pe_out, pexp, pexp->vq2, weights, sparse_pe_in); + split_vq(sparse_pe_out, pexp, pexp->vq3, weights, sparse_pe_in); + split_vq(sparse_pe_out, pexp, pexp->vq4, weights, sparse_pe_in); + split_vq(sparse_pe_out, pexp, pexp->vq5, weights, sparse_pe_in); + + /* transform quantised phases back */ + + for(i=1; i<=model->L; i++) { + pred = pexp->phi_prev[i] + N*i*best_Wo; + + index = MAX_AMP*i*model->Wo/PI; + assert(index < MAX_AMP); + error_q_rect = sparse_pe_out[index]; + error_q_angle = atan2(error_q_rect.imag, error_q_rect.real); + model->phi[i] = pred - error_q_angle; + model->phi[i] = atan2(sin(model->phi[i]), cos(model->phi[i])); + } +} + + +/* + est delta (in Wo) + see if this gives us a better (smaller variance) prediction error +*/ + +static void print_pred_error_sparse_wo_correction(struct PEXP *pexp, + MODEL *model, + int start, int end, + float mag_thresh) +{ + int i, index; + float mag, pred, error[MAX_AMP], diff, c, s, delta, err; + float sparse_pe[MAX_AMP]; + + mag = 0.0; + for(i=start; i<=end; i++) + mag += model->A[i]*model->A[i]; + mag = 10*log10(mag/(end-start)); + + if (mag > mag_thresh) { + for(i=0; iphi[i] = pexp->phi_prev[i] + N*i*(model->Wo + pexp->Wo_prev)/2.0 + 0.01*N*i; + pred = pexp->phi_prev[i] + N*i*(model->Wo + pexp->Wo_prev)/2.0; + error[i] = pred - model->phi[i]; + } + + /* estimate delta Wo */ + + c = s = 0.0; + for(i=start+1; i<=end; i++) { + diff = error[i] - error[i-1]; + c += log(model->A[i])*cos(diff); + s += log(model->A[i])*sin(diff); + } + delta = atan2(s,c)/N; + //printf("delta %f\n",delta); + delta = 0; + /* now predict phases using corrected Wo */ + + for(i=start; i<=end; i++) { + pred = pexp->phi_prev[i] + N*i*(model->Wo + pexp->Wo_prev)/2.0 - N*i*delta; + err = pred - model->phi[i]; + err = atan2(sin(err),cos(err)); + + index = MAX_AMP*i*model->Wo/PI; + assert(index < MAX_AMP); + sparse_pe[index] = err; + } + + /* dump spare phase vector in polar format */ + + for(i=0; iA[i]*model->A[i]; + mag = 10*log10(mag/(end-start)); + + if (mag > mag_thresh) { + + best_Wo = refine_Wo(pexp, model, start, end); + + /* now predict phases using corrected Wo */ + + for(i=0; iphi_prev[i] + N*i*best_Wo; + err = pred - model->phi[i]; + err = atan2(sin(err),cos(err)); + + index = MAX_AMP*i*model->Wo/PI; + assert(index < MAX_AMP); + sparse_pe[index] = err; + } + + /* dump spare phase vector in polar format */ + + for(i=0; iL); + + for(i=start; i<=end; i++) { + model->phi[i] = pexp->phi_prev[i] + N*i*best_Wo; + } +} + + +/* + This functions tests theory that some bands can be combined together + due to less frequency resolution at higher frequencies. This will + reduce the amount of information we need to encode. +*/ + +void smooth_phase(struct PEXP *pexp, MODEL *model, int mode) +{ + int m, i, j, index, step, v, en, nav, st; + COMP sparse_pe_in[MAX_AMP], av; + COMP sparse_pe_out[MAX_AMP]; + COMP smoothed[MAX_AMP]; + float best_Wo, pred, err; + float weights[MAX_AMP]; + float avw, smoothed_weights[MAX_AMP]; + COMP smoothed_in[MAX_AMP], smoothed_out[MAX_AMP]; + + best_Wo = refine_Wo(pexp, model, 1, model->L); + + for(m=0; mL; m++) { + pred = pexp->phi_prev[m] + N*m*best_Wo; + err = model->phi[m] - pred; + err = atan2(sin(err),cos(err)); + + index = MAX_AMP*m*model->Wo/PI; + assert(index < MAX_AMP); + sparse_pe_in[index].real = model->A[m]*cos(err); + sparse_pe_in[index].imag = model->A[m]*sin(err); + sparse_pe_out[index] = sparse_pe_in[index]; + weights[index] = model->A[m]; + } + + /* now combine samples at high frequencies to reduce dimension */ + + step = 2; + st = 0; + for(i=st,v=0; i (MAX_AMP-1)) + en = MAX_AMP-1; + for(j=i; jvq1, smoothed_weights, smoothed_in); + for(i=0; i (MAX_AMP-1)) + en = MAX_AMP-1; + for(j=i; jL; m++) { + index = MAX_AMP*m*model->Wo/PI; + assert(index < MAX_AMP); + pred = pexp->phi_prev[m] + N*m*best_Wo; + err = atan2(sparse_pe_out[index].imag, sparse_pe_out[index].real); + model->phi[m] = pred + err; + } + +} + +/* + Another version of a functions that tests the theory that some bands + can be combined together due to less frequency resolution at higher + frequencies. This will reduce the amount of information we need to + encode. +*/ + +void smooth_phase2(struct PEXP *pexp, MODEL *model) { + float m; + float step; + int a,b,h,i; + float best_Wo, pred, err, s,c, phi1_; + + best_Wo = refine_Wo(pexp, model, 1, model->L); + + step = (float)model->L/30; + printf("\nL: %d step: %3.2f am,bm: ", model->L, step); + for(m=(float)model->L/4; m<=model->L; m+=step) { + a = floor(m); + b = floor(m+step); + if (b > model->L) b = model->L; + h = b-a; + + printf("%d,%d,(%d) ", a, b, h); + c = s = 0.0; + if (h>1) { + for(i=a; iphi_prev[i] + N*i*best_Wo; + err = model->phi[i] - pred; + c += cos(err); s += sin(err); + } + phi1_ = atan2(s,c); + for(i=a; iphi_prev[i] + N*i*best_Wo; + printf("%d: %4.3f -> ", i, model->phi[i]); + model->phi[i] = pred + phi1_; + model->phi[i] = atan2(sin(model->phi[i]),cos(model->phi[i])); + printf("%4.3f ", model->phi[i]); + } + } + } +} + + +#define MAX_BINS 40 +//static float bins[] = {2600.0, 2800.0, 3000.0, 3200.0, 3400.0, 3600.0, 3800.0, 4000.0}; +static float bins[] = {/* + + 1000.0, 1100.0, 1200.0, 1300.0, 1400.0, + 1500.0, 1600.0, 1700.0, 1800.0, 1900.0,*/ + + 2000.0, 2400.0, 2800.0, + 3000.0, 3400.0, 3600.0, 4000.0}; + +void smooth_phase3(struct PEXP *pexp, MODEL *model) { + int m, i; + int nbins; + int b; + float f, best_Wo, pred, err; + COMP av[MAX_BINS]; + + nbins = sizeof(bins)/sizeof(float); + best_Wo = refine_Wo(pexp, model, 1, model->L); + + /* clear all bins */ + + for(i=0; iL; m++) { + f = m*model->Wo*FS/TWO_PI; + if (f > bins[0]) { + + /* find bin */ + + for(i=0; i bins[i]) && (f <= bins[i+1])) + b = i; + assert(b < MAX_BINS); + + /* est predicted phase from average */ + + pred = pexp->phi_prev[m] + N*m*best_Wo; + err = model->phi[m] - pred; + av[b].real += cos(err); av[b].imag += sin(err); + } + + } + + /* use averages to est phases */ + + for(m=1; m<=model->L; m++) { + f = m*model->Wo*FS/TWO_PI; + if (f > bins[0]) { + + /* find bin */ + + for(i=0; i bins[i]) && (f <= bins[i+1])) + b = i; + assert(b < MAX_BINS); + + /* add predicted phase error to this bin */ + + printf("L %d m %d f %4.f b %d\n", model->L, m, f, b); + + pred = pexp->phi_prev[m] + N*m*best_Wo; + err = atan2(av[b].imag, av[b].real); + printf(" %d: %4.3f -> ", m, model->phi[m]); + model->phi[m] = pred + err; + model->phi[m] = atan2(sin(model->phi[m]),cos(model->phi[m])); + printf("%4.3f\n", model->phi[m]); + } + } + printf("\n"); +} + + +/* + Try to code the phase of the largest amplitude in each band. Randomise the + phase of the other harmonics. The theory is that only the largest harmonic + will be audible. +*/ + +void cb_phase1(struct PEXP *pexp, MODEL *model) { + int m, i; + int nbins; + int b; + float f, best_Wo; + float max_val[MAX_BINS]; + int max_ind[MAX_BINS]; + + nbins = sizeof(bins)/sizeof(float); + best_Wo = refine_Wo(pexp, model, 1, model->L); + + for(i=0; iL; m++) { + f = m*model->Wo*FS/TWO_PI; + if (f > bins[0]) { + + /* find bin */ + + for(i=0; i bins[i]) && (f <= bins[i+1])) + b = i; + assert(b < MAX_BINS); + + if (model->A[m] > max_val[b]) { + max_val[b] = model->A[m]; + max_ind[b] = m; + } + } + + } + + /* randomise phase of other harmonics */ + + for(m=1; m<=model->L; m++) { + f = m*model->Wo*FS/TWO_PI; + if (f > bins[0]) { + + /* find bin */ + + for(i=0; i bins[i]) && (f <= bins[i+1])) + b = i; + assert(b < MAX_BINS); + + if (m != max_ind[b]) + model->phi[m] = pexp->phi_prev[m] + N*m*best_Wo; + } + } +} + + +/* + Theory is only the phase of the envelope of signal matters within a + Critical Band. So we estimate the position of an impulse that + approximates the envelope of the signal. +*/ + +void cb_phase2(struct PEXP *pexp, MODEL *model) { + int st, m, i, a, b, step; + float diff,w,c,s,phi1_; + float A[MAX_AMP]; + + for(m=1; m<=model->L; m++) { + A[m] = model->A[m]; + model->A[m] = 0; + } + + st = 2*model->L/4; + step = 3; + model->phi[1] = pexp->phi_prev[1] + (pexp->Wo_prev+model->Wo)*N/2.0; + + printf("L=%d ", model->L); + for(m=st; m model->L) + b = model->L; + + c = s = 0; + for(i=a; iphi[i+1] - model->phi[i]; + //w = (model->A[i+1] + model->A[i])/2; + w = 1.0; + c += w*cos(diff); s += w*sin(diff); + } + phi1_ = atan2(s,c); + printf("replacing: "); + for(i=a; iphi[i] = i*phi1_; + //model->phi[i] = i*model->phi[1]; + //model->phi[i] = m*(pexp->Wo_prev+model->Wo)*N/2.0; + model->A[m] = A[m]; + printf("%d ", i); + } + printf(" . "); + } + printf("\n"); +} + + +static void smooth_phase4(MODEL *model) { + int m; + float phi_m, phi_m_1; + + if (model->L > 25) { + printf("\nL %d\n", model->L); + for(m=model->L/2; m<=model->L; m+=2) { + if ((m+1) <= model->L) { + phi_m = (model->phi[m] - model->phi[m+1])/2.0; + phi_m_1 = (model->phi[m+1] - model->phi[m])/2.0; + model->phi[m] = phi_m; + model->phi[m+1] = phi_m_1; + printf("%d %4.3f %4.3f ", m, phi_m, phi_m_1); + } + } + } + +} + +/* try repeating last frame, just advance phases to account for time shift */ + +static void repeat_phases(struct PEXP *pexp, MODEL *model) { + int m; + + *model = pexp->prev_model; + for(m=1; m<=model->L; m++) + model->phi[m] += N*m*model->Wo; + +} + +/*---------------------------------------------------------------------------*\ + + phase_experiment() + + Phase quantisation experiments. + +\*---------------------------------------------------------------------------*/ + +void phase_experiment(struct PEXP *pexp, MODEL *model, char *arg) { + int m; + float before[MAX_AMP], best_Wo; + + assert(pexp != NULL); + memcpy(before, &model->phi[0], sizeof(float)*MAX_AMP); + + if (strcmp(arg,"q3") == 0) { + quant_phases(model, 1, model->L, 3); + update_snr_calc(pexp, model, before); + update_variance_calc(pexp, model, before); + } + + if (strcmp(arg,"dec2") == 0) { + if ((pexp->frames % 2) != 0) { + predict_phases(pexp, model, 1, model->L); + update_snr_calc(pexp, model, before); + update_variance_calc(pexp, model, before); + } + } + + if (strcmp(arg,"repeat") == 0) { + if ((pexp->frames % 2) != 0) { + repeat_phases(pexp, model); + update_snr_calc(pexp, model, before); + update_variance_calc(pexp, model, before); + } + } + + if (strcmp(arg,"vq") == 0) { + sparse_vq_pred_error(pexp, model); + update_snr_calc(pexp, model, before); + update_variance_calc(pexp, model, before); + } + + if (strcmp(arg,"pred") == 0) + predict_phases_state(pexp, model, 1, model->L); + + if (strcmp(arg,"pred1k") == 0) + predict_phases(pexp, model, 1, model->L/4); + + if (strcmp(arg,"smooth") == 0) { + smooth_phase(pexp, model,0); + update_snr_calc(pexp, model, before); + } + if (strcmp(arg,"smoothtrain") == 0) + smooth_phase(pexp, model,1); + if (strcmp(arg,"smoothvq") == 0) { + smooth_phase(pexp, model,2); + update_snr_calc(pexp, model, before); + } + + if (strcmp(arg,"smooth2") == 0) + smooth_phase2(pexp, model); + if (strcmp(arg,"smooth3") == 0) + smooth_phase3(pexp, model); + if (strcmp(arg,"smooth4") == 0) + smooth_phase4(model); + if (strcmp(arg,"vqsmooth3") == 0) { + sparse_vq_pred_error(pexp, model); + smooth_phase3(pexp, model); + } + + if (strcmp(arg,"cb1") == 0) { + cb_phase1(pexp, model); + update_snr_calc(pexp, model, before); + } + + if (strcmp(arg,"top") == 0) { + //top_amp(pexp, model, 1, model->L/4, 4, 1); + //top_amp(pexp, model, model->L/4, model->L/3, 4, 1); + //top_amp(pexp, model, model->L/3+1, model->L/2, 4, 1); + //top_amp(pexp, model, model->L/2, model->L, 6, 1); + //rand_phases(model, model->L/2, 3*model->L/4); + //struct_phases(pexp, model, model->L/2, 3*model->L/4); + //update_snr_calc(pexp, model, before); + } + + if (strcmp(arg,"pred23") == 0) { + predict_phases2(pexp, model, model->L/2, model->L); + update_snr_calc(pexp, model, before); + } + + if (strcmp(arg,"struct23") == 0) { + struct_phases(pexp, model, model->L/2, 3*model->L/4 ); + update_snr_calc(pexp, model, before); + } + + if (strcmp(arg,"addnoise") == 0) { + int m; + float max; + + max = 0; + for(m=1; m<=model->L; m++) + if (model->A[m] > max) + max = model->A[m]; + max = 20.0*log10(max); + for(m=1; m<=model->L; m++) + if (20.0*log10(model->A[m]) < (max-20)) { + model->phi[m] += (PI/4)*(1.0 -2.0*rand()/RAND_MAX); + //printf("m %d\n", m); + } + } + + /* normalise phases */ + + for(m=1; m<=model->L; m++) + model->phi[m] = atan2(sin(model->phi[m]), cos(model->phi[m])); + + /* update states */ + + //best_Wo = refine_Wo(pexp, model, model->L/2, model->L); + pexp->phi1 += N*model->Wo; + + for(m=1; m<=model->L; m++) + pexp->phi_prev[m] = model->phi[m]; + pexp->Wo_prev = model->Wo; + pexp->frames++; + pexp->prev_model = *model; +} + +#ifdef OLD_STUFF + //quant_phases(model, 1, model->L, 3); + //update_variance_calc(pexp, model, before); + //print_sparse_pred_error(pexp, model, 1, model->L, 40.0); + + //sparse_vq_pred_error(pexp, model); + + //quant_phases(model, model->L/4+1, model->L, 3); + + //predict_phases1(pexp, model, 1, model->L/4); + //quant_phases(model, model->L/4+1, model->L, 3); + + //quant_phases(model, 1, model->L/8, 3); + + //update_snr_calc(pexp, model, before); + //update_variance_calc(pexp, model, before); + + //fixed_bits_per_frame(pexp, model, 40); + //struct_phases(pexp, model, 1, model->L/4); + //rand_phases(model, 10, model->L); + //for(m=1; m<=model->L; m++) + // model->A[m] = 0.0; + //model->A[model->L/2] = 1000; + //repeat_phases(model, 20); + //predict_phases(pexp, model, 1, model->L/4); + //quant_phases(model, 1, 10, 3); + //quant_phases(model, 10, 20, 2); + //repeat_phases(model, 20); + //rand_phases(model, 3*model->L/4, model->L); + // print_phi1_pred_error(model, 1, model->L); + //predict_phases(pexp, model, 1, model->L/4); + //first_order_band(model, model->L/4, model->L/2); + //first_order_band(model, model->L/2, 3*model->L/4); + //if (fabs(model->Wo - pexp->Wo_prev)< 0.1*model->Wo) + + //print_pred_error(pexp, model, 1, model->L, 40.0); + //print_sparse_pred_error(pexp, model, 1, model->L, 40.0); + + //phi1_est = est_phi1(model, 1, model->L/4); + //print_phi1_pred_error(model, 1, model->L/4); + + //first_order_band(model, 1, model->L/4, phi1_est); + //sub_linear(model, 1, model->L/4, phi1_est); + + //top_amp(pexp, model, 1, model->L/4, 4); + //top_amp(pexp, model, model->L/4, model->L/2, 4); + + //first_order_band(model, 1, model->L/4, phi1_est); + //first_order_band(model, model->L/4, model->L/2, phi1_est); + + //if (fabs(model->Wo - pexp->Wo_prev) > 0.2*model->Wo) + // rand_phases(model, model->L/2, model->L); + + //top_amp(pexp, model, 1, model->L/4, 4); + //top_amp(pexp, model, model->L/4, model->L/2, 8); + //top_amp(pexp, model, model->L/4+1, model->L/2, 10, 1); + //top_amp(pexp, model, 1, model->L/4, 10, 1); + //top_amp(pexp, model, model->L/4+1, 3*model->L/4, 10, 1); + //top_amp(pexp, model, 1, 3*model->L/4, 20, 1); + + #ifdef REAS_CAND1 + predict_phases(pexp, model, 1, model->L/4); + top_amp(pexp, model, model->L/4+1, 3*model->L/4, 10, 1); + rand_phases(model, 3*model->L/4+1, model->L); + #endif + + #ifdef REAS_CAND2 + if ((pexp->frames % 2) == 0) { + //printf("quant\n"); + predict_phases(pexp, model, 1, model->L/4); + //top_amp(pexp, model, model->L/4+1, 3*model->L/4, 20, 1); + top_amp(pexp, model, model->L/4+1, 7*model->L/8, 20, 1); + rand_phases(model, 7*model->L/8+1, model->L); + } + else { + //printf("predict\n"); + predict_phases(pexp, model, 1, model->L); + } + #endif + + //#define REAS_CAND3 + #ifdef REAS_CAND3 + if ((pexp->frames % 3) != 0) { + printf("pred\n"); + predict_phases(pexp, model, 1, model->L); + } + else { + predict_phases(pexp, model, 1, model->L/4); + fixed_bits_per_frame(pexp, model, model->L/4+1, 60); + } + #endif + //predict_phases(pexp, model, model->L/4, model->L); + + + //print_pred_error(pexp, model, 1, model->L); + //limit_prediction_error(pexp, model, model->L/2, model->L, PI/2); +#endif diff --git a/libs/libcodec2/src/phaseexp.h b/libs/libcodec2/src/phaseexp.h new file mode 100644 index 0000000000..b43db75e83 --- /dev/null +++ b/libs/libcodec2/src/phaseexp.h @@ -0,0 +1,39 @@ +/*---------------------------------------------------------------------------*\ + + FILE........: phaseexp.h + AUTHOR......: David Rowe + DATE CREATED: June 2012 + + Experimental functions for quantising, modelling and synthesising phase. + +\*---------------------------------------------------------------------------*/ + +/* + Copyright (C) 2012 David Rowe + + All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 2.1, as + published by the Free Software Foundation. This program is + distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program; if not, see . +*/ + +#ifndef __PHASEEXP__ +#define __PHASEEXP__ + +#include "kiss_fft.h" + +struct PEXP; + +struct PEXP * phase_experiment_create(); +void phase_experiment_destroy(struct PEXP *pexp); +void phase_experiment(struct PEXP *pexp, MODEL *model, char *arg); + +#endif diff --git a/libs/libcodec2/src/pilot_coeff.h b/libs/libcodec2/src/pilot_coeff.h new file mode 100644 index 0000000000..66e7501d8f --- /dev/null +++ b/libs/libcodec2/src/pilot_coeff.h @@ -0,0 +1,34 @@ +/* Generated by pilot_coeff_file() Octave function */ + +const float pilot_coeff[]={ + 0.00204705, + 0.00276339, + 0.00432595, + 0.00697042, + 0.0108452, + 0.0159865, + 0.0223035, + 0.029577, + 0.0374709, + 0.045557, + 0.0533491, + 0.0603458, + 0.0660751, + 0.070138, + 0.0722452, + 0.0722452, + 0.070138, + 0.0660751, + 0.0603458, + 0.0533491, + 0.045557, + 0.0374709, + 0.029577, + 0.0223035, + 0.0159865, + 0.0108452, + 0.00697042, + 0.00432595, + 0.00276339, + 0.00204705 +}; diff --git a/libs/libcodec2/src/postfilter.c b/libs/libcodec2/src/postfilter.c index 6dad76b1e1..c78f495bed 100644 --- a/libs/libcodec2/src/postfilter.c +++ b/libs/libcodec2/src/postfilter.c @@ -24,15 +24,16 @@ License for more details. You should have received a copy of the GNU Lesser General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program; if not, see . */ +#include #include #include #include #include "defines.h" +#include "comp.h" #include "dump.h" #include "postfilter.h" @@ -44,6 +45,11 @@ #define BG_THRESH 40.0 /* only consider low levels signals for bg_est */ #define BG_BETA 0.1 /* averaging filter constant */ +#define BG_MARGIN 6.0 /* harmonics this far above BG noise are + randomised. Helped make bg noise less + spikey (impulsive) for mmt1, but speech was + perhaps a little rougher. + */ /*---------------------------------------------------------------------------*\ @@ -61,7 +67,7 @@ (5-12) are required to transmit the frequency selective voicing information. Mixed excitation also requires accurate voicing estimation (parameter estimators always break occasionally under - exceptional condition). + exceptional conditions). In our case we use a post filter approach which requires no additional bits to be transmitted. The decoder measures the average @@ -105,6 +111,7 @@ void postfilter( for(m=1; m<=model->L; m++) e += model->A[m]*model->A[m]; + assert(e > 0.0); e = 10.0*log10(e/model->L); /* If beneath threhold, update bg estimate. The idea @@ -121,11 +128,13 @@ void postfilter( uv = 0; if (model->voiced) for(m=1; m<=model->L; m++) - if (20.0*log10(model->A[m]) < *bg_est) { + if (20.0*log10(model->A[m]) < (*bg_est + BG_MARGIN)) { model->phi[m] = TWO_PI*(float)rand()/RAND_MAX; uv++; } +#ifdef DUMP dump_bg(e, *bg_est, 100.0*uv/model->L); +#endif } diff --git a/libs/libcodec2/src/postfilter.h b/libs/libcodec2/src/postfilter.h index d4dd4ae053..bf080b1b65 100644 --- a/libs/libcodec2/src/postfilter.h +++ b/libs/libcodec2/src/postfilter.h @@ -22,8 +22,7 @@ License for more details. You should have received a copy of the GNU Lesser General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program; if not, see . */ #ifndef __POSTFILTER__ diff --git a/libs/libcodec2/src/quantise.c b/libs/libcodec2/src/quantise.c index a1cd728112..1153943b9f 100644 --- a/libs/libcodec2/src/quantise.c +++ b/libs/libcodec2/src/quantise.c @@ -20,8 +20,8 @@ License for more details. You should have received a copy of the GNU Lesser General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program; if not, see . + */ #include @@ -36,270 +36,705 @@ #include "quantise.h" #include "lpc.h" #include "lsp.h" -#include "four1.h" -#include "codebook.h" +#include "kiss_fft.h" #define LSP_DELTA1 0.01 /* grid spacing for LSP root searches */ -#define MAX_CB 20 /* max number of codebooks */ -/* describes each codebook */ +/*---------------------------------------------------------------------------*\ + + FUNCTION HEADERS -typedef struct { - int k; /* dimension of vector */ - int log2m; /* number of bits in m */ - int m; /* elements in codebook */ - float *fn; /* file name of text file storing the VQ */ -} LSP_CB; +\*---------------------------------------------------------------------------*/ -/* lsp_q describes entire quantiser made up of several codebooks */ +float speech_to_uq_lsps(float lsp[], float ak[], float Sn[], float w[], + int order); -#ifdef OLDER -/* 10+10+6+6 = 32 bit LSP difference split VQ */ +/*---------------------------------------------------------------------------*\ + + FUNCTIONS -LSP_CB lsp_q[] = { - {3, 1024, "/usr/src/freeswitch/libs/libcodec2-1.0/unittest/lspd123.txt"}, - {3, 1024, "/usr/src/freeswitch/libs/libcodec2-1.0/unittest/lspd456.txt"}, - {2, 64, "/usr/src/freeswitch/libs/libcodec2-1.0/unittest/lspd78.txt"}, - {2, 64, "/usr/src/freeswitch/libs/libcodec2-1.0/unittest/lspd910.txt"}, - {0, 0, ""} -}; -#endif +\*---------------------------------------------------------------------------*/ -LSP_CB lsp_q[] = { - {1,4,16, codebook_lsp1 }, - {1,4,16, codebook_lsp2 }, - {1,4,16, codebook_lsp3 }, - {1,4,16, codebook_lsp4 }, - {1,4,16, codebook_lsp5 }, - {1,4,16, codebook_lsp6 }, - {1,4,16, codebook_lsp7 }, - {1,3,8, codebook_lsp8 }, - {1,3,8, codebook_lsp9 }, - {1,2,4, codebook_lsp10 }, - {0,0,0, NULL }, -}; +int lsp_bits(int i) { + return lsp_cb[i].log2m; +} -/* ptr to each codebook */ +int lspd_bits(int i) { + return lsp_cbd[i].log2m; +} + +int lspdt_bits(int i) { + return lsp_cbdt[i].log2m; +} -static float *plsp_cb[MAX_CB]; +int lsp_pred_vq_bits(int i) { + return lsp_cbjvm[i].log2m; +} /*---------------------------------------------------------------------------*\ - - FUNCTION HEADERS + + quantise_init + + Loads the entire LSP quantiser comprised of several vector quantisers + (codebooks). \*---------------------------------------------------------------------------*/ -float speech_to_uq_lsps(float lsp[], float ak[], float Sn[], float w[], - int order); +void quantise_init() +{ +} /*---------------------------------------------------------------------------*\ - - FUNCTIONS + + quantise + + Quantises vec by choosing the nearest vector in codebook cb, and + returns the vector index. The squared error of the quantised vector + is added to se. \*---------------------------------------------------------------------------*/ -int lsp_bits(int i) { - return lsp_q[i].log2m; +long quantise(const float * cb, float vec[], float w[], int k, int m, float *se) +/* float cb[][K]; current VQ codebook */ +/* float vec[]; vector to quantise */ +/* float w[]; weighting vector */ +/* int k; dimension of vectors */ +/* int m; size of codebook */ +/* float *se; accumulated squared error */ +{ + float e; /* current error */ + long besti; /* best index so far */ + float beste; /* best error so far */ + long j; + int i; + float diff; + + besti = 0; + beste = 1E32; + for(j=0; j max) val[0] = max; + k = lsp_cbd[i].k; + cb = lsp_cbd[i].cb; + dlsp_[i] = cb[indexes[i]*k]; - norm = (*val - min)/(max-min); - printf("%f norm: %f ", val[0], norm); - index = fabs(levels*norm + 0.5); + if (i) + lsp__hz[i] = lsp__hz[i-1] + dlsp_[i]; + else + lsp__hz[0] = dlsp_[0]; - *val = min + index*(max-min)/levels; + lsp_[i] = (PI/4000.0)*lsp__hz[i]; + + //printf("%d dlsp_ %3.2f lsp_ %3.2f\n", i, dlsp_[i], lsp__hz[i]); + } - printf("index %d val_: %f\n", index, val[0]); } + /*---------------------------------------------------------------------------*\ - lspd_quantise + lspvq_quantise - Simulates differential lsp quantiser + Vector LSP quantiser. \*---------------------------------------------------------------------------*/ -void lsp_quantise( +void lspvq_quantise( float lsp[], float lsp_[], int order ) { - int i; - float dlsp[LPC_MAX]; - float dlsp_[LPC_MAX]; + int i,k,m,ncb, nlsp; + float wt[LPC_ORD], lsp_hz[LPC_ORD]; + const float *cb; + float se; + int index; + + for(i=0; i 0); + mbest = (struct MBEST *)malloc(sizeof(struct MBEST)); + assert(mbest != NULL); + + mbest->entries = entries; + mbest->list = (struct MBEST_LIST *)malloc(entries*sizeof(struct MBEST_LIST)); + assert(mbest->list != NULL); + + for(i=0; ientries; i++) { + for(j=0; jlist[i].index[j] = 0; + mbest->list[i].error = 1E32; } + + return mbest; +} + + +static void mbest_destroy(struct MBEST *mbest) { + assert(mbest != NULL); + free(mbest->list); + free(mbest); } + /*---------------------------------------------------------------------------*\ - quantise_init + mbest_insert - Loads the entire LSP quantiser comprised of several vector quantisers - (codebooks). + Insert the results of a vector to codebook entry comparison. The + list is ordered in order or error, so those entries with the + smallest error will be first on the list. \*---------------------------------------------------------------------------*/ -void quantise_init() -{ - int i,k,m; +static void mbest_insert(struct MBEST *mbest, int index[], float error) { + int i, j, found; + struct MBEST_LIST *list = mbest->list; + int entries = mbest->entries; + + found = 0; + for(i=0; ii; j--) + list[j] = list[j-1]; + for(j=0; jentries; i++) { + for(j=0; jlist[i].index[j]); + printf(" %f\n", mbest->list[i].error); } } + /*---------------------------------------------------------------------------*\ - quantise + mbest_search - Quantises vec by choosing the nearest vector in codebook cb, and - returns the vector index. The squared error of the quantised vector - is added to se. + Searches vec[] to a codebbook of vectors, and maintains a list of the mbest + closest matches. \*---------------------------------------------------------------------------*/ -long quantise(float cb[], float vec[], float w[], int k, int m, float *se) -/* float cb[][K]; current VQ codebook */ -/* float vec[]; vector to quantise */ -/* float w[]; weighting vector */ -/* int k; dimension of vectors */ -/* int m; size of codebook */ -/* float *se; accumulated squared error */ +static void mbest_search( + const float *cb, /* VQ codebook to search */ + float vec[], /* target vector */ + float w[], /* weighting vector */ + int k, /* dimension of vector */ + int m, /* number on entries in codebook */ + struct MBEST *mbest, /* list of closest matches */ + int index[] /* indexes that lead us here */ +) { - float e; /* current error */ - long besti; /* best index so far */ - float beste; /* best error so far */ - long j; - int i; + float e; + int i,j; + float diff; - besti = 0; - beste = 1E32; for(j=0; jlist[j].index[0]; + for(i=0; ilist[j].index[1]; + index[1] = n2 = mbest_stage2->list[j].index[0]; + for(i=0; ilist[j].index[2]; + index[2] = n2 = mbest_stage3->list[j].index[1]; + index[1] = n3 = mbest_stage3->list[j].index[0]; + for(i=0; ilist[0].index[3]; + n2 = mbest_stage4->list[0].index[2]; + n3 = mbest_stage4->list[0].index[1]; + n4 = mbest_stage4->list[0].index[0]; + for (i=0;i {Am} LPC decode */ + aks_to_M2(ak,order,model,E,&snr, 1, 0, 1); /* {ak} -> {Am} LPC decode */ return snr; } +#endif /*---------------------------------------------------------------------------*\ - aks_to_M2() - - Transforms the linear prediction coefficients to spectral amplitude - samples. This function determines A(m) from the average energy per - band using an FFT. - + lpc_post_filter() + + Applies a post filter to the LPC synthesis filter power spectrum + Pw, which supresses the inter-formant energy. + + The algorithm is from p267 (Section 8.6) of "Digital Speech", + edited by A.M. Kondoz, 1994 published by Wiley and Sons. Chapter 8 + of this text is on the MBE vocoder, and this is a freq domain + adaptation of post filtering commonly used in CELP. + + I used the Octave simulation lpcpf.m to get an understaing of the + algorithm. + + Requires two more FFTs which is significantly more MIPs. However + it should be possible to implement this more efficiently in the + time domain. Just not sure how to handle relative time delays + between the synthesis stage and updating these coeffs. A smaller + FFT size might also be accetable to save CPU. + + TODO: + [ ] sync var names between Octave and C version + [ ] doc gain normalisation + [ ] I think the first FFT is not rqd as we do the same + thing in aks_to_M2(). + \*---------------------------------------------------------------------------*/ -void aks_to_M2( - float ak[], /* LPC's */ - int order, - MODEL *model, /* sinusoidal model parameters for this frame */ - float E, /* energy term */ - float *snr, /* signal to noise ratio for this frame in dB */ - int dump /* true to dump sample to dump file */ +void lpc_post_filter(kiss_fft_cfg fft_fwd_cfg, MODEL *model, COMP Pw[], float ak[], + int order, int dump, float beta, float gamma, int bass_boost) +{ + int i; + COMP x[FFT_ENC]; /* input to FFTs */ + COMP Aw[FFT_ENC]; /* LPC analysis filter spectrum */ + COMP Ww[FFT_ENC]; /* weighting spectrum */ + float Rw[FFT_ENC]; /* R = WA */ + float e_before, e_after, gain; + float Pfw[FFT_ENC]; /* Post filter mag spectrum */ + float max_Rw, min_Rw; + float range, thresh, r, w; + int m, bin; + + /* Determine LPC inverse filter spectrum 1/A(exp(jw)) -----------*/ + + /* we actually want the synthesis filter A(exp(jw)) but the + inverse (analysis) filter is easier to find as it's FIR, we + just use the inverse of 1/A to get the synthesis filter + A(exp(jw)) */ + + for(i=0; i max_Rw) + max_Rw = Rw[i]; + if (Rw[i] < min_Rw) + min_Rw = Rw[i]; + + } + #ifdef DUMP + if (dump) + dump_Rw(Rw); + #endif + + /* create post filter mag spectrum and apply ------------------*/ + + /* measure energy before post filtering */ + + e_before = 1E-4; + for(i=0; iL; m++) { am = floor((m - 0.5)*model->Wo/r + 0.5); bm = floor((m + 0.5)*model->Wo/r + 0.5); @@ -494,6 +1083,22 @@ void aks_to_M2( signal += pow(model->A[m],2.0); noise += pow(model->A[m] - Am,2.0); + + /* This code significantly improves perf of LPC model, in + particular when combined with phase0. The LPC spectrum tends + to track just under the peaks of the spectral envelope, and + just above nulls. This algorithm does the reverse to + compensate - raising the amplitudes of spectral peaks, while + attenuating the null. This enhances the formants, and + supresses the energy between formants. */ + + if (sim_pf) { + if (Am > model->A[m]) + Am *= 0.7; + if (Am < model->A[m]) + Am *= 1.4; + } + model->A[m] = Am; } *snr = 10.0*log10(signal/noise); @@ -547,6 +1152,84 @@ float decode_Wo(int index) return Wo; } +/*---------------------------------------------------------------------------*\ + + FUNCTION....: encode_Wo_dt() + AUTHOR......: David Rowe + DATE CREATED: 6 Nov 2011 + + Encodes Wo difference from last frame. + +\*---------------------------------------------------------------------------*/ + +int encode_Wo_dt(float Wo, float prev_Wo) +{ + int index, mask, max_index, min_index; + float Wo_min = TWO_PI/P_MAX; + float Wo_max = TWO_PI/P_MIN; + float norm; + + norm = (Wo - prev_Wo)/(Wo_max - Wo_min); + index = floor(WO_LEVELS * norm + 0.5); + //printf("ENC index: %d ", index); + + /* hard limit */ + + max_index = (1 << (WO_DT_BITS-1)) - 1; + min_index = - (max_index+1); + if (index > max_index) index = max_index; + if (index < min_index) index = min_index; + //printf("max_index: %d min_index: %d hard index: %d ", + // max_index, min_index, index); + + /* mask so that only LSB WO_DT_BITS remain, bit WO_DT_BITS is the sign bit */ + + mask = ((1 << WO_DT_BITS) - 1); + index &= mask; + //printf("mask: 0x%x index: 0x%x\n", mask, index); + + return index; +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: decode_Wo_dt() + AUTHOR......: David Rowe + DATE CREATED: 6 Nov 2011 + + Decodes Wo using WO_DT_BITS difference from last frame. + +\*---------------------------------------------------------------------------*/ + +float decode_Wo_dt(int index, float prev_Wo) +{ + float Wo_min = TWO_PI/P_MAX; + float Wo_max = TWO_PI/P_MIN; + float step; + float Wo; + int mask; + + /* sign extend index */ + + //printf("DEC index: %d "); + if (index & (1 << (WO_DT_BITS-1))) { + mask = ~((1 << WO_DT_BITS) - 1); + index |= mask; + } + //printf("DEC mask: 0x%x index: %d \n", mask, index); + + step = (Wo_max - Wo_min)/WO_LEVELS; + Wo = prev_Wo + step*(index); + + /* bit errors can make us go out of range leading to all sorts of + probs like seg faults */ + + if (Wo > Wo_max) Wo = Wo_max; + if (Wo < Wo_min) Wo = Wo_min; + + return Wo; +} + /*---------------------------------------------------------------------------*\ FUNCTION....: speech_to_uq_lsps() @@ -569,40 +1252,65 @@ float speech_to_uq_lsps(float lsp[], int i, roots; float Wn[M]; float R[LPC_MAX+1]; - float E; + float e, E; - for(i=0; iA[1]) - 20.0*log10(tmp.A[1])); - if (E1 > 6.0) - return 1; - else - return 0; + step = 100; + for(i=4; i<10; i++) { + lsp_hz = lsps[i]*4000.0/PI; + lsp_hz = floor(lsp_hz/step + 0.5)*step; + lsps[i] = lsp_hz*PI/4000.0; + if (i) { + if (lsps[i] == lsps[i-1]) + lsps[i] += step*PI/4000.0; + + } + } } + /*---------------------------------------------------------------------------*\ FUNCTION....: apply_lpc_correction() AUTHOR......: David Rowe DATE CREATED: 22/8/2010 - Apply first harmonic LPC correction at decoder. + Apply first harmonic LPC correction at decoder. This helps improve + low pitch males after LPC modelling, like hts1a and morig. \*---------------------------------------------------------------------------*/ -void apply_lpc_correction(MODEL *model, int lpc_correction) +void apply_lpc_correction(MODEL *model) { - if (lpc_correction) { - if (model->Wo < (PI*150.0/4000)) { - model->A[1] *= 0.032; - } + if (model->Wo < (PI*150.0/4000)) { + model->A[1] *= 0.032; } } @@ -787,7 +1782,7 @@ int encode_energy(float e) AUTHOR......: David Rowe DATE CREATED: 22/8/2010 - Decodes energy using a WO_BITS quantiser. + Decodes energy using a E_LEVELS quantiser. \*---------------------------------------------------------------------------*/ @@ -805,36 +1800,7 @@ float decode_energy(int index) return e; } -/*---------------------------------------------------------------------------*\ - - FUNCTION....: encode_amplitudes() - AUTHOR......: David Rowe - DATE CREATED: 22/8/2010 - - Time domain LPC is used model the amplitudes which are then - converted to LSPs and quantised. So we don't actually encode the - amplitudes directly, rather we derive an equivalent representation - from the time domain speech. - -\*---------------------------------------------------------------------------*/ - -void encode_amplitudes(int lsp_indexes[], - int *lpc_correction, - int *energy_index, - MODEL *model, - float Sn[], - float w[]) -{ - float lsps[LPC_ORD]; - float ak[LPC_ORD+1]; - float e; - - e = speech_to_uq_lsps(lsps, ak, Sn, w, LPC_ORD); - encode_lsps(lsp_indexes, lsps, LPC_ORD); - *lpc_correction = need_lpc_correction(model, ak, e); - *energy_index = encode_energy(e); -} - +#ifdef NOT_USED /*---------------------------------------------------------------------------*\ FUNCTION....: decode_amplitudes() @@ -846,23 +1812,222 @@ void encode_amplitudes(int lsp_indexes[], \*---------------------------------------------------------------------------*/ -float decode_amplitudes(MODEL *model, +float decode_amplitudes(kiss_fft_cfg fft_fwd_cfg, + MODEL *model, float ak[], int lsp_indexes[], - int lpc_correction, - int energy_index + int energy_index, + float lsps[], + float *e ) { - float lsps[LPC_ORD]; - float e; float snr; - decode_lsps(lsps, lsp_indexes, LPC_ORD); + decode_lsps_scalar(lsps, lsp_indexes, LPC_ORD); bw_expand_lsps(lsps, LPC_ORD); lsp_to_lpc(lsps, ak, LPC_ORD); - e = decode_energy(energy_index); - aks_to_M2(ak, LPC_ORD, model, e, &snr, 1); - apply_lpc_correction(model, lpc_correction); + *e = decode_energy(energy_index); + aks_to_M2(ak, LPC_ORD, model, *e, &snr, 1, 0, 0, 1); + apply_lpc_correction(model); return snr; } +#endif + +static float ge_coeff[2] = {0.8, 0.9}; + +void compute_weights2(const float *x, const float *xp, float *w, int ndim) +{ + w[0] = 30; + w[1] = 1; + if (x[1]<0) + { + w[0] *= .6; + w[1] *= .3; + } + if (x[1]<-10) + { + w[0] *= .3; + w[1] *= .3; + } + /* Higher weight if pitch is stable */ + if (fabs(x[0]-xp[0])<.2) + { + w[0] *= 2; + w[1] *= 1.5; + } else if (fabs(x[0]-xp[0])>.5) /* Lower if not stable */ + { + w[0] *= .5; + } + + /* Lower weight for low energy */ + if (x[1] < xp[1]-10) + { + w[1] *= .5; + } + if (x[1] < xp[1]-20) + { + w[1] *= .5; + } + + //w[0] = 30; + //w[1] = 1; + + /* Square the weights because it's applied on the squared error */ + w[0] *= w[0]; + w[1] *= w[1]; + +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: quantise_WoE() + AUTHOR......: Jean-Marc Valin & David Rowe + DATE CREATED: 29 Feb 2012 + + Experimental joint Wo and LPC energy vector quantiser developed by + Jean-Marc Valin. Exploits correlations between the difference in + the log pitch and log energy from frame to frame. For example + both the pitch and energy tend to only change by small amounts + during voiced speech, however it is important that these changes be + coded carefully. During unvoiced speech they both change a lot but + the ear is less sensitve to errors so coarser quantisation is OK. + + The ear is sensitive to log energy and loq pitch so we quantise in + these domains. That way the error measure used to quantise the + values is close to way the ear senses errors. + + See http://jmspeex.livejournal.com/10446.html + +\*---------------------------------------------------------------------------*/ + +void quantise_WoE(MODEL *model, float *e, float xq[]) +{ + int i, n1; + float x[2]; + float err[2]; + float w[2]; + const float *codebook1 = ge_cb[0].cb; + int nb_entries = ge_cb[0].m; + int ndim = ge_cb[0].k; + float Wo_min = TWO_PI/P_MAX; + float Wo_max = TWO_PI/P_MIN; + + x[0] = log10((model->Wo/PI)*4000.0/50.0)/log10(2); + x[1] = 10.0*log10(1e-4 + *e); + + compute_weights2(x, xq, w, ndim); + for (i=0;iWo = pow(2.0, xq[0])*(PI*50.0)/4000.0; + + /* bit errors can make us go out of range leading to all sorts of + probs like seg faults */ + + if (model->Wo > Wo_max) model->Wo = Wo_max; + if (model->Wo < Wo_min) model->Wo = Wo_min; + + model->L = PI/model->Wo; /* if we quantise Wo re-compute L */ + + *e = pow(10.0, xq[1]/10.0); +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: encode_WoE() + AUTHOR......: Jean-Marc Valin & David Rowe + DATE CREATED: 11 May 2012 + + Joint Wo and LPC energy vector quantiser developed my Jean-Marc + Valin. Returns index, and updated states xq[]. + +\*---------------------------------------------------------------------------*/ + +int encode_WoE(MODEL *model, float e, float xq[]) +{ + int i, n1; + float x[2]; + float err[2]; + float w[2]; + const float *codebook1 = ge_cb[0].cb; + int nb_entries = ge_cb[0].m; + int ndim = ge_cb[0].k; + + assert((1<Wo/PI)*4000.0/50.0)/log10(2); + x[1] = 10.0*log10(1e-4 + e); + + compute_weights2(x, xq, w, ndim); + for (i=0;iWo = pow(2.0, xq[0])*(PI*50.0)/4000.0; + + /* bit errors can make us go out of range leading to all sorts of + probs like seg faults */ + + if (model->Wo > Wo_max) model->Wo = Wo_max; + if (model->Wo < Wo_min) model->Wo = Wo_min; + + model->L = PI/model->Wo; /* if we quantise Wo re-compute L */ + + *e = pow(10.0, xq[1]/10.0); +} + diff --git a/libs/libcodec2/src/quantise.h b/libs/libcodec2/src/quantise.h index ded7645381..1f5f9ee788 100644 --- a/libs/libcodec2/src/quantise.h +++ b/libs/libcodec2/src/quantise.h @@ -20,65 +20,104 @@ License for more details. You should have received a copy of the GNU Lesser General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program; if not, see . */ #ifndef __QUANTISE__ #define __QUANTISE__ -#define WO_BITS 7 -#define WO_LEVELS (1<. */ /*---------------------------------------------------------------------------*\ @@ -38,7 +37,9 @@ #include "defines.h" #include "sine.h" -#include "four1.h" +#include "kiss_fft.h" + +#define HPF_BETA 0.125 /*---------------------------------------------------------------------------*\ @@ -65,9 +66,10 @@ void hs_pitch_refinement(MODEL *model, COMP Sw[], float pmin, float pmax, \*---------------------------------------------------------------------------*/ -void make_analysis_window(float w[],COMP W[]) +void make_analysis_window(kiss_fft_cfg fft_fwd_cfg, float w[], COMP W[]) { float m; + COMP wshift[FFT_ENC]; COMP temp; int i,j; @@ -122,15 +124,15 @@ void make_analysis_window(float w[],COMP W[]) */ for(i=0; iL/4; l++) { sig += model->A[l]*model->A[l]; } - for(i=0; iWo; + error = 1E-4; /* Just test across the harmonics in the first 1000 Hz (L/4) */ - for(l=1; l<=L/4; l++) { + for(l=1; l<=model->L/4; l++) { Am.real = 0.0; Am.imag = 0.0; den = 0.0; @@ -418,16 +441,73 @@ float est_voicing_mbe( offset = FFT_ENC/2 + m - l*Wo*FFT_ENC/TWO_PI + 0.5; Sw_[m].real = Am.real*W[offset].real - Am.imag*W[offset].imag; Sw_[m].imag = Am.real*W[offset].imag + Am.imag*W[offset].real; - error += (Sw[m].real - Sw_[m].real)*(Sw[m].real - Sw_[m].real); - error += (Sw[m].imag - Sw_[m].imag)*(Sw[m].imag - Sw_[m].imag); + Ew[m].real = Sw[m].real - Sw_[m].real; + Ew[m].imag = Sw[m].imag - Sw_[m].imag; + error += Ew[m].real*Ew[m].real; + error += Ew[m].imag*Ew[m].imag; } } - + snr = 10.0*log10(sig/error); if (snr > V_THRESH) model->voiced = 1; else model->voiced = 0; + + /* post processing, helps clean up some voicing errors ------------------*/ + + /* + Determine the ratio of low freqency to high frequency energy, + voiced speech tends to be dominated by low frequency energy, + unvoiced by high frequency. This measure can be used to + determine if we have made any gross errors. + */ + + elow = ehigh = 1E-4; + for(l=1; l<=model->L/2; l++) { + elow += model->A[l]*model->A[l]; + } + for(l=model->L/2; l<=model->L; l++) { + ehigh += model->A[l]*model->A[l]; + } + eratio = 10.0*log10(elow/ehigh); + dF0 = 0.0; + + /* Look for Type 1 errors, strongly V speech that has been + accidentally declared UV */ + + if (model->voiced == 0) + if (eratio > 10.0) + model->voiced = 1; + + /* Look for Type 2 errors, strongly UV speech that has been + accidentally declared V */ + + if (model->voiced == 1) { + if (eratio < -10.0) + model->voiced = 0; + + /* If pitch is jumping about it's likely this is UV */ + + /* 13 Feb 2012 - this seems to add some V errors so comment out for now. Maybe + double check on bg noise files + + dF0 = (model->Wo - prev_Wo)*FS/TWO_PI; + if (fabs(dF0) > 15.0) + model->voiced = 0; + */ + + /* A common source of Type 2 errors is the pitch estimator + gives a low (50Hz) estimate for UV speech, which gives a + good match with noise due to the close harmoonic spacing. + These errors are much more common than people with 50Hz3 + pitch, so we have just a small eratio threshold. */ + + sixty = 60.0*TWO_PI/FS; + if ((eratio < -4.0) && (model->Wo <= sixty)) + model->voiced = 0; + } + //printf(" v: %d snr: %f eratio: %3.2f %f\n",model->voiced,snr,eratio,dF0); return snr; } @@ -471,20 +551,22 @@ void make_synthesis_window(float Pn[]) DATE CREATED: 20/2/95 Synthesise a speech signal in the frequency domain from the - sinusodal model parameters. Uses overlap-add a triangular window to - smoothly interpolate betwen frames. + sinusodal model parameters. Uses overlap-add with a trapezoidal + window to smoothly interpolate betwen frames. \*---------------------------------------------------------------------------*/ void synthesise( - float Sn_[], /* time domain synthesised signal */ - MODEL *model, /* ptr to model parameters for this frame */ - float Pn[], /* time domain Parzen window */ - int shift /* used to handle transition frames */ + kiss_fft_cfg fft_inv_cfg, + float Sn_[], /* time domain synthesised signal */ + MODEL *model, /* ptr to model parameters for this frame */ + float Pn[], /* time domain Parzen window */ + int shift /* flag used to handle transition frames */ ) { int i,l,j,b; /* loop variables */ COMP Sw_[FFT_DEC]; /* DFT of synthesised signal */ + COMP sw_[FFT_DEC]; /* synthesised signal */ if (shift) { /* Update memories */ @@ -500,10 +582,30 @@ void synthesise( Sw_[i].imag = 0.0; } + /* + Nov 2010 - found that synthesis using time domain cos() functions + gives better results for synthesis frames greater than 10ms. Inverse + FFT synthesis using a 512 pt FFT works well for 10ms window. I think + (but am not sure) that the problem is related to the quantisation of + the harmonic frequencies to the FFT bin size, e.g. there is a + 8000/512 Hz step between FFT bins. For some reason this makes + the speech from longer frame > 10ms sound poor. The effect can also + be seen when synthesising test signals like single sine waves, some + sort of amplitude modulation at the frame rate. + + Another possibility is using a larger FFT size (1024 or 2048). + */ + +#define FFT_SYNTHESIS +#ifdef FFT_SYNTHESIS /* Now set up frequency domain synthesised speech */ - for(l=1; l<=model->L; l++) { + //for(l=model->L/2; l<=model->L; l++) { + //for(l=1; l<=model->L/4; l++) { b = floor(l*model->Wo*FFT_DEC/TWO_PI + 0.5); + if (b > ((FFT_DEC/2)-1)) { + b = (FFT_DEC/2)-1; + } Sw_[b].real = model->A[l]*cos(model->phi[l]); Sw_[b].imag = model->A[l]*sin(model->phi[l]); Sw_[FFT_DEC-b].real = Sw_[b].real; @@ -512,19 +614,35 @@ void synthesise( /* Perform inverse DFT */ - four1(&Sw_[-1].imag,FFT_DEC,1); + kiss_fft(fft_inv_cfg, (kiss_fft_cpx *)Sw_, (kiss_fft_cpx *)sw_); +#else + /* + Direct time domain synthesis using the cos() function. Works + well at 10ms and 20ms frames rates. Note synthesis window is + still used to handle overlap-add between adjacent frames. This + could be simplified as we don't need to synthesise where Pn[] + is zero. + */ + for(l=1; l<=model->L; l++) { + for(i=0,j=-N+1; iA[l]*cos(j*model->Wo*l + model->phi[l]); + } + for(i=N-1,j=0; i<2*N; i++,j++) + Sw_[j].real += 2.0*model->A[l]*cos(j*model->Wo*l + model->phi[l]); + } +#endif /* Overlap add to previous samples */ for(i=0; i. */ #ifndef __SINE__ #define __SINE__ -void make_analysis_window(float w[], COMP W[]); -void dft_speech(COMP Sw[], float Sn[], float w[]); +#include "defines.h" +#include "comp.h" +#include "kiss_fft.h" + +void make_analysis_window(kiss_fft_cfg fft_fwd_cfg, float w[], COMP W[]); +float hpf(float x, float states[]); +void dft_speech(kiss_fft_cfg fft_fwd_cfg, COMP Sw[], float Sn[], float w[]); void two_stage_pitch_refinement(MODEL *model, COMP Sw[]); void estimate_amplitudes(MODEL *model, COMP Sw[], COMP W[]); -float est_voicing_mbe(MODEL *model, COMP Sw[], COMP W[], float f0, COMP Sw_[]); +float est_voicing_mbe(MODEL *model, COMP Sw[], COMP W[], COMP Sw_[],COMP Ew[], + float prev_Wo); void make_synthesis_window(float Pn[]); -void synthesise(float Sn_[], MODEL *model, float Pn[], int shift); +void synthesise(kiss_fft_cfg fft_inv_cfg, float Sn_[], MODEL *model, float Pn[], int shift); #endif diff --git a/libs/libcodec2/src/test_bits.h b/libs/libcodec2/src/test_bits.h new file mode 100644 index 0000000000..19d7a92f62 --- /dev/null +++ b/libs/libcodec2/src/test_bits.h @@ -0,0 +1,116 @@ +/* Generated by test_bits_file() Octave function */ + +const int test_bits[]={ + 0, + 1, + 1, + 0, + 0, + 0, + 1, + 1, + 0, + 0, + 1, + 0, + 1, + 0, + 0, + 1, + 0, + 1, + 1, + 0, + 0, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 1, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 1, + 1, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 1, + 0, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 1, + 1, + 0, + 1, + 0, + 0, + 0, + 1, + 1, + 1, + 0 +}; diff --git a/libs/libcodec2/unittest/Makefile.am b/libs/libcodec2/unittest/Makefile.am index e18b44d5e4..df6e327261 100644 --- a/libs/libcodec2/unittest/Makefile.am +++ b/libs/libcodec2/unittest/Makefile.am @@ -1,41 +1,105 @@ -AM_CFLAGS = -I../src -g -DFLOATING_POINT -DVAR_ARRAYS -AUTOMAKE_OPTIONS = gnu +AM_CFLAGS = -I../src -fPIC -g -DFLOATING_POINT -DVAR_ARRAYS -O2 -Wall +AUTOMAKE_OPTS = gnu NAME = libcodec2 AM_CPPFLAGS = $(AM_CFLAGS) -bin_PROGRAMS = genres genlsp extract vqtrain tnlp tinterp tquant tcodec2 +noinst_PROGRAMS = genres genlsp extract vqtrain vqtrainjnd tnlp tinterp tquant vq_train_jvm scalarlsptest tfdmdv t48_8 lspsync create_interleaver tlspsens vqtrainph genphdata genampdata polar2rect vqtrainsp tprede pre de tfifo - -genres_SOURCES = genres.c ../src/lpc.c ../src/codebook.c +genres_SOURCES = genres.c ../src/lpc.c genres_LDADD = $(lib_LTLIBRARIES) genres_LDFLAGS = $(LIBS) -genlsp_SOURCES = genlsp.c ../src/lpc.c ../src/lsp.c ../src/codebook.c +genlsp_SOURCES = genlsp.c ../src/lpc.c ../src/lsp.c genlsp_LDADD = $(lib_LTLIBRARIES) genlsp_LDFLAGS = $(LIBS) -extract_SOURCES = extract.c ../src/codebook.c +extract_SOURCES = extract.c extract_LDADD = $(lib_LTLIBRARIES) extract_LDFLAGS = $(LIBS) -vqtrain_SOURCES = vqtrain.c ../src/codebook.c +vqtrain_SOURCES = vqtrain.c vqtrain_LDADD = $(lib_LTLIBRARIES) vqtrain_LDFLAGS = $(LIBS) -tnlp_SOURCES = tnlp.c ../src/sine.c ../src/nlp.c ../src/four1.c ../src/dump.c ../src/codebook.c +vqtrainjnd_SOURCES = vqtrainjnd.c +vqtrainjnd_LDADD = $(lib_LTLIBRARIES) +vqtrainjnd_LDFLAGS = $(LIBS) + +vqtrainph_SOURCES = vqtrainph.c +vqtrainph_LDADD = $(lib_LTLIBRARIES) +vqtrainph_LDFLAGS = $(LIBS) + +vqtrainsp_SOURCES = vqtrainsp.c +vqtrainsp_LDADD = $(lib_LTLIBRARIES) +vqtrainsp_LDFLAGS = $(LIBS) + +genphdata_SOURCES = genphdata.c +genphdata_LDADD = $(lib_LTLIBRARIES) +genphdata_LDFLAGS = $(LIBS) + +genampdata_SOURCES = genampdata.c +genampdata_LDADD = $(lib_LTLIBRARIES) +genampdata_LDFLAGS = $(LIBS) + +polar2rect_SOURCES = polar2rect.c +polar2rect_LDADD = $(lib_LTLIBRARIES) +polar2rect_LDFLAGS = $(LIBS) + +vq_train_jvm_SOURCES = vq_train_jvm.c +vq_train_jvm_LDADD = $(lib_LTLIBRARIES) +vq_train_jvm_LDFLAGS = $(LIBS) + +CODEBOOKS = ../src/codebook.c ../src/codebookd.c ../src/codebookvq.c ../src/codebookjnd.c ../src/codebookdt.c ../src/codebookjvm.c ../src/codebookvqanssi.c ../src/codebookge.c + +tnlp_SOURCES = tnlp.c ../src/sine.c ../src/nlp.c ../src/kiss_fft.c ../src/dump.c tnlp_LDADD = $(lib_LTLIBRARIES) tnlp_LDFLAGS = $(LIBS) -tinterp_SOURCES = tinterp.c ../src/sine.c ../src/four1.c ../src/interp.c ../src/codebook.c +tinterp_SOURCES = tinterp.c ../src/sine.c ../src/kiss_fft.c ../src/interp.c ../src/lpc.c ../src/lsp.c ../src/quantise.c $(CODEBOOKS) ../src/dump.c tinterp_LDADD = $(lib_LTLIBRARIES) tinterp_LDFLAGS = $(LIBS) -tquant_SOURCES = tquant.c ../src/quantise.c ../src/lpc.c ../src/lsp.c ../src/dump.c ../src/four1.c ../src/codebook.c +tquant_SOURCES = tquant.c ../src/quantise.c ../src/lpc.c ../src/lsp.c ../src/dump.c ../src/kiss_fft.c $(CODEBOOKS) tquant_LDADD = $(lib_LTLIBRARIES) tquant_LDFLAGS = $(LIBS) -tcodec2_SOURCES = tcodec2.c ../src/quantise.c ../src/lpc.c ../src/lsp.c ../src/dump.c ../src/four1.c \ -../src/codec2.c ../src/sine.c ../src/nlp.c ../src/postfilter.c ../src/phase.c ../src/interp.c ../src/pack.c ../src/codebook.c -tcodec2_LDADD = $(lib_LTLIBRARIES) -tcodec2_LDFLAGS = $(LIBS) +scalarlsptest_SOURCES = scalarlsptest.c ../src/quantise.c ../src/lpc.c ../src/lsp.c ../src/dump.c ../src/kiss_fft.c $(CODEBOOKS) +scalarlsptest_LDADD = $(lib_LTLIBRARIES) +scalarlsptest_LDFLAGS = $(LIBS) + +tfdmdv_SOURCES = tfdmdv.c ../src/fdmdv.c ../src/kiss_fft.c ../src/octave.c +tfdmdv_LDADD = $(lib_LTLIBRARIES) +tfdmdv_LDFLAGS = $(LIBS) + +t48_8_SOURCES = t48_8.c ../src/fdmdv.c ../src/kiss_fft.c +t48_8_LDADD = $(lib_LTLIBRARIES) +t48_8_LDFLAGS = $(LIBS) + +lspsync_SOURCES = lspsync.c ../src/quantise.c ../src/lpc.c ../src/lsp.c ../src/dump.c ../src/kiss_fft.c \ +../src/codec2.c ../src/sine.c ../src/nlp.c ../src/postfilter.c ../src/phase.c ../src/interp.c ../src/pack.c $(CODEBOOKS) +lspsync_LDADD = $(lib_LTLIBRARIES) +lspsync_LDFLAGS = $(LIBS) + +create_interleaver_SOURCES = create_interleaver.c +create_interleaver_LDADD = $(lib_LTLIBRARIES) +create_interleaver_LDFLAGS = $(LIBS) + +tlspsens_SOURCES = tlspsens.c ../src/quantise.c ../src/lpc.c ../src/lsp.c ../src/dump.c ../src/kiss_fft.c ../src/codec2.c ../src/sine.c ../src/nlp.c ../src/pack.c ../src/interp.c ../src/postfilter.c ../src/phase.c $(CODEBOOKS) +tlspsens_LDADD = $(lib_LTLIBRARIES) +tlspsens_LDFLAGS = $(LIBS) + +tprede_SOURCES = tprede.c ../src/lpc.c +tprede_LDADD = $(lib_LTLIBRARIES) +tprede_LDFLAGS = $(LIBS) + +pre_SOURCES = pre.c ../src/lpc.c +pre_LDADD = $(lib_LTLIBRARIES) +pre_LDFLAGS = $(LIBS) + +de_SOURCES = de.c ../src/lpc.c +de_LDADD = $(lib_LTLIBRARIES) +de_LDFLAGS = $(LIBS) +tfifo_SOURCES = tfifo.c ../src/fifo.c +tfifo_LDADD = $(lib_LTLIBRARIES) -lpthread +tfifo_LDFLAGS = $(LIBS) diff --git a/libs/libcodec2/unittest/extract.c b/libs/libcodec2/unittest/extract.c index b7544edf3a..2812d55ec4 100644 --- a/libs/libcodec2/unittest/extract.c +++ b/libs/libcodec2/unittest/extract.c @@ -25,15 +25,16 @@ License for more details. You should have received a copy of the GNU Lesser General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program; if not, see . */ -#define MAX_STR 256 /* maximum string length */ +#define MAX_STR 2048 /* maximum string length */ #include #include +#include #include +#include void scan_line(FILE *fp, float f[], int n); @@ -45,8 +46,8 @@ int main(int argc, char *argv[]) { long lines; /* lines read so far */ if (argc != 5) { - printf("usage: extract TextFile FloatFile start end\n"); - exit(0); + printf("usage: %s TextFile FloatFile start(1 .. 10) end(1 .. 10)\n", argv[0]); + exit(1); } /* read command line arguments and open files */ @@ -75,8 +76,10 @@ int main(int argc, char *argv[]) { lines = 0; while(!feof(ftext)) { scan_line(ftext, buf, en); - fwrite(&buf[st-1], sizeof(float), en-st+1, ffloat); - printf("\r%ld lines",lines++); + if (!feof(ftext)) { + fwrite(&buf[st-1], sizeof(float), en-st+1, ffloat); + printf("\r%ld lines",++lines); + } } printf("\n"); @@ -108,9 +111,11 @@ void scan_line(FILE *fp, float f[], int n) char s[MAX_STR]; char *ps,*pe; int i; - - fgets(s,MAX_STR,fp); - ps = pe = s; + + memset(s, 0, MAX_STR); + ps = pe = fgets(s,MAX_STR,fp); + if (ps == NULL) + return; for(i=0; i. */ -#define P 10 /* LP order */ -#define LSP_DELTA1 0.05 /* grid spacing for LSP root searches */ +#define P 12 /* LP order */ +#define LSP_DELTA1 0.01 /* grid spacing for LSP root searches */ #define NW 279 /* frame size in samples */ #define N 80 /* frame to frame shift */ #define THRESH 40.0 /* threshold energy/sample for frame inclusion */ +#define PI 3.141592654 /* mathematical constant */ #include #include @@ -61,19 +61,22 @@ int main(int argc, char *argv[]) { float Sn[NW]; /* float input speech samples */ float ak[P+1]; /* LPCs for current frame */ float lsp[P]; /* LSPs for current frame */ + float lsp_prev[P]; /* LSPs for previous frame */ float E; /* frame energy */ + long f; /* number of frames */ long af; /* number frames with "active" speech */ float Eres; /* LPC residual energy */ int i; int roots; int unstables; - int lspd; + int lspd, log, lspdt; + float diff; /* Initialise ------------------------------------------------------*/ if (argc < 3) { - printf("usage: gentest RawFile LSPTextFile [--lspd]\n"); - exit(0); + printf("usage: %s RawFile LSPTextFile [--lspd] [--log] [--lspdt] \n", argv[0]); + exit(1); } /* Open files */ @@ -91,13 +94,15 @@ int main(int argc, char *argv[]) { } lspd = switch_present("--lspd", argc, argv); + log = switch_present("--log", argc, argv); + lspdt = switch_present("--lspdt", argc, argv); for(i=0; i THRESH) { af++; printf("Active Frame: %ld unstables: %d\n",af, unstables); find_aks(Sn, ak, NW, P, &Eres); - roots = lpc_to_lsp(&ak[1], P , lsp, 5, LSP_DELTA1); + roots = lpc_to_lsp(ak, P , lsp, 5, LSP_DELTA1); if (roots == P) { if (lspd) { - fprintf(flsp,"%f ",lsp[0]); - for(i=1; i. */ #include @@ -47,7 +46,7 @@ int main(int argc, char *argv[]) if (argc < 3) { printf("usage: %s InputFile ResidualFile\n", argv[0]); - exit(0); + exit(1); } /* Open files */ diff --git a/libs/libcodec2/unittest/sd.c b/libs/libcodec2/unittest/sd.c index f77b5099d5..74bec67df7 100644 --- a/libs/libcodec2/unittest/sd.c +++ b/libs/libcodec2/unittest/sd.c @@ -22,8 +22,7 @@ License for more details. You should have received a copy of the GNU Lesser General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program; if not, see . */ #define MAX_N 2048 /* maximum DFT size */ diff --git a/libs/libcodec2/unittest/sd.h b/libs/libcodec2/unittest/sd.h new file mode 100644 index 0000000000..c92e0f703e --- /dev/null +++ b/libs/libcodec2/unittest/sd.h @@ -0,0 +1,33 @@ +/*--------------------------------------------------------------------------*\ + + FILE........: sd.h + AUTHOR......: David Rowe + DATE CREATED: 22/7/93 + + Function to determine spectral distortion between two sets of LPCs. + +\*--------------------------------------------------------------------------*/ + +/* + Copyright (C) 2009 David Rowe + + All rights reserved. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 2, as + published by the Free Software Foundation. This program is + distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program; if not, see . +*/ + +#ifndef __SD__ +#define __SD__ + +float spectral_dist(float ak1[], float ak2[], int p, int n); + +#endif /* __SD__ */ diff --git a/libs/libcodec2/unittest/tcodec2.c b/libs/libcodec2/unittest/tcodec2.c index 33806fc7d3..8624ce8af7 100644 --- a/libs/libcodec2/unittest/tcodec2.c +++ b/libs/libcodec2/unittest/tcodec2.c @@ -22,8 +22,7 @@ License for more details. You should have received a copy of the GNU Lesser General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program; if not, see . */ #include @@ -32,39 +31,43 @@ #include #include #include "defines.h" +#include "comp.h" #include "codec2.h" #include "quantise.h" #include "interp.h" /* CODEC2 struct copies from codec2.c to help with testing */ -typedef struct { - float Sn[M]; /* input speech */ - float w[M]; /* time domain hamming window */ - COMP W[FFT_ENC]; /* DFT of w[] */ - float Pn[2*N]; /* trapezoidal synthesis window */ - float Sn_[2*N]; /* synthesised speech */ - float prev_Wo; /* previous frame's pitch estimate */ - float ex_phase; /* excitation model phase track */ - float bg_est; /* background noise estimate for post filter */ - MODEL prev_model; /* model parameters from 20ms ago */ -} CODEC2; - -void analyse_one_frame(CODEC2 *c2, MODEL *model, short speech[]); -void synthesise_one_frame(CODEC2 *c2, short speech[], MODEL *model, float ak[]); +struct CODEC2 { + int mode; + float w[M]; /* time domain hamming window */ + COMP W[FFT_ENC]; /* DFT of w[] */ + float Pn[2*N]; /* trapezoidal synthesis window */ + float Sn[M]; /* input speech */ + float hpf_states[2]; /* high pass filter states */ + void *nlp; /* pitch predictor states */ + float Sn_[2*N]; /* synthesised output speech */ + float ex_phase; /* excitation model phase track */ + float bg_est; /* background noise estimate for post filter */ + float prev_Wo; /* previous frame's pitch estimate */ + MODEL prev_model; /* previous frame's model parameters */ + float prev_lsps_[LPC_ORD]; /* previous frame's LSPs */ + float prev_energy; /* previous frame's LPC energy */ +}; + +void analyse_one_frame(struct CODEC2 *c2, MODEL *model, short speech[]); +void synthesise_one_frame(struct CODEC2 *c2, short speech[], MODEL *model, float ak[]); int test1() { FILE *fin, *fout; short buf[N]; - void *c2; - CODEC2 *c3; + struct CODEC2 *c2; MODEL model; float ak[LPC_ORD+1]; float lsps[LPC_ORD]; - c2 = codec2_create(); - c3 = (CODEC2*)c2; + c2 = codec2_create(CODEC2_MODE_2400); fin = fopen("../raw/hts1a.raw", "rb"); assert(fin != NULL); @@ -72,9 +75,9 @@ int test1() assert(fout != NULL); while(fread(buf, sizeof(short), N, fin) == N) { - analyse_one_frame(c3, &model, buf); - speech_to_uq_lsps(lsps, ak, c3->Sn, c3->w, LPC_ORD); - synthesise_one_frame(c3, buf, &model, ak); + analyse_one_frame(c2, &model, buf); + speech_to_uq_lsps(lsps, ak, c2->Sn, c2->w, LPC_ORD); + synthesise_one_frame(c2, buf, &model, ak); fwrite(buf, sizeof(short), N, fout); } @@ -90,22 +93,22 @@ int test2() { FILE *fin, *fout; short buf[2*N]; - void *c2; - CODEC2 *c3; + struct CODEC2 *c2; MODEL model, model_interp; float ak[LPC_ORD+1]; int voiced1, voiced2; int lsp_indexes[LPC_ORD]; - int lpc_correction; int energy_index; int Wo_index; - char bits[CODEC2_BITS_PER_FRAME]; + char *bits; int nbit; int i; - - c2 = codec2_create(); - c3 = (CODEC2*)c2; - + float lsps[LPC_ORD]; + float e; + + c2 = codec2_create(CODEC2_MODE_2400); + bits = (char*)malloc(codec2_bits_per_frame(c2)); + assert(bits != NULL); fin = fopen("../raw/hts1a.raw", "rb"); assert(fin != NULL); fout = fopen("hts1a_test.raw", "wb"); @@ -114,60 +117,57 @@ int test2() while(fread(buf, sizeof(short), 2*N, fin) == 2*N) { /* first 10ms analysis frame - we just want voicing */ - analyse_one_frame(c3, &model, buf); + analyse_one_frame(c2, &model, buf); voiced1 = model.voiced; /* second 10ms analysis frame */ - analyse_one_frame(c3, &model, &buf[N]); + analyse_one_frame(c2, &model, &buf[N]); voiced2 = model.voiced; Wo_index = encode_Wo(model.Wo); - encode_amplitudes(lsp_indexes, - &lpc_correction, - &energy_index, - &model, - c3->Sn, - c3->w); + e = speech_to_uq_lsps(lsps, ak, c2->Sn, c2->w, LPC_ORD); + encode_lsps_scalar(lsp_indexes, lsps, LPC_ORD); + energy_index = encode_energy(e); nbit = 0; - pack(bits, &nbit, Wo_index, WO_BITS); + pack((unsigned char*)bits, (unsigned *)&nbit, Wo_index, WO_BITS); for(i=0; iprev_model, &model); + interpolate(&model_interp, &c2->prev_model, &model); - synthesise_one_frame(c3, buf, &model_interp, ak); - synthesise_one_frame(c3, &buf[N], &model, ak); + synthesise_one_frame(c2, buf, &model_interp, ak); + synthesise_one_frame(c2, &buf[N], &model, ak); - memcpy(&c3->prev_model, &model, sizeof(MODEL)); + memcpy(&c2->prev_model, &model, sizeof(MODEL)); fwrite(buf, sizeof(short), 2*N, fout); } + free(bits); codec2_destroy(c2); fclose(fin); @@ -181,10 +181,14 @@ int test3() FILE *fin, *fout, *fbits; short buf1[2*N]; short buf2[2*N]; - char bits[CODEC2_BITS_PER_FRAME]; - void *c2; + char *bits; + struct CODEC2 *c2; + + c2 = codec2_create(CODEC2_MODE_2400); + int numBits = codec2_bits_per_frame(c2); + int numBytes = (numBits+7)>>3; - c2 = codec2_create(); + bits = (char*)malloc(numBytes); fin = fopen("../raw/hts1a.raw", "rb"); assert(fin != NULL); @@ -194,12 +198,13 @@ int test3() assert(fout != NULL); while(fread(buf1, sizeof(short), 2*N, fin) == 2*N) { - codec2_encode(c2, bits, buf1); - fwrite(bits, sizeof(char), CODEC2_BITS_PER_FRAME, fbits); - codec2_decode(c2, buf2, bits); - fwrite(buf2, sizeof(short), CODEC2_SAMPLES_PER_FRAME, fout); + codec2_encode(c2, (void*)bits, buf1); + fwrite(bits, sizeof(char), numBytes, fbits); + codec2_decode(c2, buf2, (void*)bits); + fwrite(buf2, sizeof(short), numBytes, fout); } + free(bits); codec2_destroy(c2); fclose(fin); diff --git a/libs/libcodec2/unittest/tcontphase.c b/libs/libcodec2/unittest/tcontphase.c index ee2f662a48..6761bac8d0 100644 --- a/libs/libcodec2/unittest/tcontphase.c +++ b/libs/libcodec2/unittest/tcontphase.c @@ -24,8 +24,7 @@ License for more details. You should have received a copy of the GNU Lesser General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program; if not, see . */ #define N 80 /* frame size */ @@ -103,8 +102,8 @@ char *argv[]; float f0; if (argc < 3) { - printf("\nusage: tcontphase OutputRawSpeechFile F0\n"); - exit(0); + printf("\nusage: %s OutputRawSpeechFile F0\n", argv[0]); + exit(1); } /* Output file */ diff --git a/libs/libcodec2/unittest/tinterp.c b/libs/libcodec2/unittest/tinterp.c index 7bb37c5258..8520c832b2 100644 --- a/libs/libcodec2/unittest/tinterp.c +++ b/libs/libcodec2/unittest/tinterp.c @@ -22,8 +22,7 @@ License for more details. You should have received a copy of the GNU Lesser General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program; if not, see . */ #include @@ -32,6 +31,9 @@ #include #include #include +#include +#include +#include #include "defines.h" #include "sine.h" @@ -60,13 +62,14 @@ void write_amp(char file[], MODEL *model) fclose(f); } -char *get_next_float(char *s, float *num) +const char *get_next_float(const char *s, float *num) { - char *p = s; + const char *p = s; char tmp[MAX_STR]; while(*p && !isspace(*p)) p++; + assert((p-s) < (int)(sizeof(tmp)-1)); memcpy(tmp, s, p-s); tmp[p-s] = 0; *num = atof(tmp); @@ -74,13 +77,14 @@ char *get_next_float(char *s, float *num) return p+1; } -char *get_next_int(char *s, int *num) +const char *get_next_int(const char *s, int *num) { - char *p = s; + const char *p = s; char tmp[MAX_STR]; while(*p && !isspace(*p)) p++; + assert((p-s) < (int)(sizeof(tmp)-1)); memcpy(tmp, s, p-s); tmp[p-s] = 0; *num = atoi(tmp); @@ -88,18 +92,20 @@ char *get_next_int(char *s, int *num) return p+1; } -void load_amp(MODEL *model, char file[], int frame) +void load_amp(MODEL *model, const char * file, int frame) { FILE *f; int i; char s[1024]; - char *ps; + const char *ps; f = fopen(file,"rt"); + assert(f); for(i=0; iWo); ps = get_next_int(ps, &model->L); @@ -109,13 +115,30 @@ void load_amp(MODEL *model, char file[], int frame) fclose(f); } +void load_or_make_amp(MODEL *model, + const char * filename, int frame, + float f0, float cdB, float mdBHz) +{ + struct stat buf; + int rc = stat(filename, &buf); + if (rc || !S_ISREG(buf.st_mode) || ((buf.st_mode & S_IRUSR) != S_IRUSR)) + { + make_amp(model, f0, cdB, mdBHz); + } + else + { + load_amp(model, filename, frame); + } +} int main() { MODEL prev, next, interp; - //make_amp(&prev, 50.0, 60.0, 6E-3); - //make_amp(&next, 50.0, 40.0, 6E-3); - load_amp(&prev, "../src/hts1a_model.txt", 32); - load_amp(&next, "../src/hts1a_model.txt", 34); + load_or_make_amp(&prev, + "../src/hts1a_model.txt", 32, + 50.0, 60.0, 6E-3); + load_or_make_amp(&next, + "../src/hts1a_model.txt", 34, + 50.0, 40.0, 6E-3); interp.voiced = 1; interpolate(&interp, &prev, &next); diff --git a/libs/libcodec2/unittest/tnlp.c b/libs/libcodec2/unittest/tnlp.c index 4abf69c4ef..d6a8735929 100644 --- a/libs/libcodec2/unittest/tnlp.c +++ b/libs/libcodec2/unittest/tnlp.c @@ -22,8 +22,7 @@ License for more details. You should have received a copy of the GNU Lesser General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program; if not, see . */ #define N 80 /* frame size */ @@ -41,6 +40,7 @@ #include "dump.h" #include "sine.h" #include "nlp.h" +#include "kiss_fft.h" int frames; @@ -81,19 +81,22 @@ char *argv[]; FILE *fin,*fout; short buf[N]; float Sn[M]; /* float input speech samples */ + kiss_fft_cfg fft_fwd_cfg; COMP Sw[FFT_ENC]; /* DFT of Sn[] */ float w[M]; /* time domain hamming window */ COMP W[FFT_ENC]; /* DFT of w[] */ float pitch; int i; - int dump; float prev_Wo; void *nlp_states; +#ifdef DUMP + int dump; +#endif if (argc < 3) { printf("\nusage: tnlp InputRawSpeechFile OutputPitchTextFile " "[--dump DumpFile]\n"); - exit(0); + exit(1); } /* Input file */ @@ -110,12 +113,18 @@ char *argv[]; exit(1); } +#ifdef DUMP dump = switch_present("--dump",argc,argv); if (dump) dump_on(argv[dump+1]); +#else +/// TODO +/// #warning "Compile with -DDUMP if you expect to dump anything." +#endif nlp_states = nlp_create(); - make_analysis_window(w,W); + fft_fwd_cfg = kiss_fft_alloc(FFT_ENC, 0, NULL, NULL); + make_analysis_window(fft_fwd_cfg, w, W); frames = 0; prev_Wo = 0; @@ -128,10 +137,12 @@ char *argv[]; Sn[i] = Sn[i+N]; for(i=0; i. */ #include @@ -123,8 +122,8 @@ int test_lsp(int lsp_number, int levels, float max_error_hz) { for(i=0; i max_error_rads) { diff --git a/libs/libcodec2/unittest/vqtrain.c b/libs/libcodec2/unittest/vqtrain.c index b46d4fcf30..86966d544b 100644 --- a/libs/libcodec2/unittest/vqtrain.c +++ b/libs/libcodec2/unittest/vqtrain.c @@ -23,8 +23,7 @@ License for more details. You should have received a copy of the GNU Lesser General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with this program; if not, see . */ /*-----------------------------------------------------------------------*\ @@ -38,6 +37,7 @@ #include #include #include +#include /*-----------------------------------------------------------------------*\ @@ -59,7 +59,7 @@ void acc(float v1[], float v2[], int k); void norm(float v[], int k, long n); long quantise(float cb[], float vec[], int k, int m, float *se); -/*-----------------------------------------------------------------------*\ +/*-----------------------------------------------------------------------* \ MAIN @@ -79,12 +79,13 @@ int main(int argc, char *argv[]) { float delta; /* improvement in distortion */ FILE *ftrain; /* file containing training set */ FILE *fvq; /* file containing vector quantiser */ + int ret; /* Interpret command line arguments */ if (argc != 5) { - printf("usage: vqtrain TrainFile K M VQFile\n"); - exit(0); + printf("usage: %s TrainFile K(dimension) M(codebook size) VQFile\n", argv[0]); + exit(1); } /* Open training file */ @@ -99,7 +100,7 @@ int main(int argc, char *argv[]) { k = atol(argv[2]); m = atol(argv[3]); - printf("dimension K=%ld number of entries M=%ld\n", k,m); + printf("dimension K=%ld number of entries M=%ld\n", k, m); vec = (float*)malloc(sizeof(float)*k); cb = (float*)malloc(sizeof(float)*k*m); cent = (float*)malloc(sizeof(float)*k*m); @@ -112,14 +113,14 @@ int main(int argc, char *argv[]) { /* determine size of training set */ J = 0; - while(fread(vec, sizeof(float), k, ftrain) == k) + while(fread(vec, sizeof(float), k, ftrain) == (size_t)k) J++; printf("J=%ld entries in training set\n", J); /* set up initial codebook state from samples of training set */ rewind(ftrain); - fread(cb, sizeof(float), k*m, ftrain); + ret = fread(cb, sizeof(float), k*m, ftrain); /* main loop */ @@ -140,7 +141,7 @@ int main(int argc, char *argv[]) { se = 0.0; rewind(ftrain); for(i=0; i DELTAQ) for(i=0; iencoder = codec2_create(); + context->encoder = codec2_create(CODEC2_MODE_2400); } if (decoding) { - context->decoder = codec2_create(); + context->decoder = codec2_create(CODEC2_MODE_2400); } codec->private_info = context;