From 36c0ba64703776f4cc2a77adc00740b05e0b055d Mon Sep 17 00:00:00 2001 From: trondeau Date: Sat, 7 Apr 2007 01:31:18 +0000 Subject: [PATCH] merged developer branch trondeau/cvsd -r4801:4904 to add CVSD vocoder and example code git-svn-id: http://gnuradio.org/svn/gnuradio/trunk@4906 221aa14e-8319-0410-a670-987f0aec2ac5 --- config/grc_gr_cvsd_vocoder.m4 | 36 ++++ configure.ac | 1 + .../python/digital_voice/Makefile.am | 3 +- .../python/digital_voice/cvsd_test.py | 67 ++++++ gr-cvsd-vocoder/Makefile.am | 25 +++ gr-cvsd-vocoder/src/Makefile.am | 22 ++ gr-cvsd-vocoder/src/lib/Makefile.am | 97 +++++++++ gr-cvsd-vocoder/src/lib/__init__.py | 20 ++ gr-cvsd-vocoder/src/lib/cvsd_decode_bs.cc | 196 ++++++++++++++++++ gr-cvsd-vocoder/src/lib/cvsd_decode_bs.h | 97 +++++++++ gr-cvsd-vocoder/src/lib/cvsd_encode_sb.cc | 194 +++++++++++++++++ gr-cvsd-vocoder/src/lib/cvsd_encode_sb.h | 96 +++++++++ gr-cvsd-vocoder/src/lib/cvsd_vocoder.i | 85 ++++++++ gr-cvsd-vocoder/src/python/Makefile.am | 40 ++++ gr-cvsd-vocoder/src/python/cvsd.py | 52 +++++ gr-cvsd-vocoder/src/python/encdec.py | 67 ++++++ gr-cvsd-vocoder/src/python/qa_cvsd_vocoder.py | 112 ++++++++++ gr-cvsd-vocoder/src/python/run_tests.in | 10 + 18 files changed, 1219 insertions(+), 1 deletion(-) create mode 100644 config/grc_gr_cvsd_vocoder.m4 create mode 100755 gnuradio-examples/python/digital_voice/cvsd_test.py create mode 100644 gr-cvsd-vocoder/Makefile.am create mode 100644 gr-cvsd-vocoder/src/Makefile.am create mode 100644 gr-cvsd-vocoder/src/lib/Makefile.am create mode 100644 gr-cvsd-vocoder/src/lib/__init__.py create mode 100644 gr-cvsd-vocoder/src/lib/cvsd_decode_bs.cc create mode 100644 gr-cvsd-vocoder/src/lib/cvsd_decode_bs.h create mode 100644 gr-cvsd-vocoder/src/lib/cvsd_encode_sb.cc create mode 100644 gr-cvsd-vocoder/src/lib/cvsd_encode_sb.h create mode 100644 gr-cvsd-vocoder/src/lib/cvsd_vocoder.i create mode 100644 gr-cvsd-vocoder/src/python/Makefile.am create mode 100644 gr-cvsd-vocoder/src/python/cvsd.py create mode 100755 gr-cvsd-vocoder/src/python/encdec.py create mode 100755 gr-cvsd-vocoder/src/python/qa_cvsd_vocoder.py create mode 100644 gr-cvsd-vocoder/src/python/run_tests.in diff --git a/config/grc_gr_cvsd_vocoder.m4 b/config/grc_gr_cvsd_vocoder.m4 new file mode 100644 index 00000000..a4f9578e --- /dev/null +++ b/config/grc_gr_cvsd_vocoder.m4 @@ -0,0 +1,36 @@ +dnl Copyright 2001,2002,2003,2004,2005,2006 Free Software Foundation, Inc. +dnl +dnl This file is part of GNU Radio +dnl +dnl GNU Radio is free software; you can redistribute it and/or modify +dnl it under the terms of the GNU General Public License as published by +dnl the Free Software Foundation; either version 2, or (at your option) +dnl any later version. +dnl +dnl GNU Radio is distributed in the hope that it will be useful, +dnl but WITHOUT ANY WARRANTY; without even the implied warranty of +dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +dnl GNU General Public License for more details. +dnl +dnl You should have received a copy of the GNU General Public License +dnl along with GNU Radio; see the file COPYING. If not, write to +dnl the Free Software Foundation, Inc., 51 Franklin Street, +dnl Boston, MA 02110-1301, USA. + +AC_DEFUN([GRC_GR_CVSD_VOCODER],[ + GRC_ENABLE([gr-cvsd-vocoder]) + + AC_CONFIG_FILES([\ + gr-cvsd-vocoder/Makefile \ + gr-cvsd-vocoder/src/Makefile \ + gr-cvsd-vocoder/src/lib/Makefile \ + gr-cvsd-vocoder/src/python/Makefile \ + gr-cvsd-vocoder/src/python/run_tests \ + ]) + + passed=yes + GRC_BUILD_CONDITIONAL([gr-cvsd-vocoder],[ + dnl run_tests is created from run_tests.in. Make it executable. + AC_CONFIG_COMMANDS([run_tests_cvsd], [chmod +x gr-cvsd-vocoder/src/python/run_tests]) + ]) +]) diff --git a/configure.ac b/configure.ac index 74259352..3bf03562 100644 --- a/configure.ac +++ b/configure.ac @@ -208,6 +208,7 @@ GRC_GR_AUDIO_PORTAUDIO GRC_GR_AUDIO_WINDOWS GRC_GR_ATSC GRC_GR_COMEDI +GRC_GR_CVSD_VOCODER GRC_GR_GSM_FR_VOCODER GRC_GR_PAGER GRC_GR_RADIO_ASTRONOMY diff --git a/gnuradio-examples/python/digital_voice/Makefile.am b/gnuradio-examples/python/digital_voice/Makefile.am index 17cd1dd7..bd8f8e7e 100644 --- a/gnuradio-examples/python/digital_voice/Makefile.am +++ b/gnuradio-examples/python/digital_voice/Makefile.am @@ -20,4 +20,5 @@ # EXTRA_DIST = \ - encdec.py + encdec.py \ + cvsd_test.py diff --git a/gnuradio-examples/python/digital_voice/cvsd_test.py b/gnuradio-examples/python/digital_voice/cvsd_test.py new file mode 100755 index 00000000..4ca7bca9 --- /dev/null +++ b/gnuradio-examples/python/digital_voice/cvsd_test.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python +# +# Copyright 2007 Free Software Foundation, Inc. +# +# This file is part of GNU Radio +# +# GNU Radio 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 2, or (at your option) +# any later version. +# +# GNU Radio 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 GNU Radio; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, +# Boston, MA 02110-1301, USA. +# + +from gnuradio import gr, blks +from gnuradio import audio +from gnuradio.eng_option import eng_option +from optparse import OptionParser + +def main(): + parser = OptionParser(option_class=eng_option) + parser.add_option("-I", "--audio-input", type="string", default="", + help="pcm input device name. E.g., hw:0,0 or /dev/dsp") + parser.add_option("-O", "--audio-output", type="string", default="", + help="pcm output device name. E.g., hw:0,0 or /dev/dsp") + parser.add_option("-r", "--sample-rate", type="eng_float", default="32000", + help="Audio sampling rate [defaul=%default]") + parser.add_option("-S", "--resample-rate", type="int", default="8", + help="Resampling rate in CVSD [default=%default]") + (options, args) = parser.parse_args () + + if len(args) != 0: + parser.print_help() + raise SystemExit, 1 + + fg = gr.flow_graph() + + src = audio.source(int(options.sample_rate), options.audio_input) + tx = blks.cvsd_encode(fg, options.resample_rate) + + # todo: add noise + + rx = blks.cvsd_decode(fg, options.resample_rate) + dst = audio.sink(int(options.sample_rate), options.audio_output) + + fg.connect(src, tx, rx, dst) + + fg.start() + + raw_input ('Press Enter to exit: ') + fg.stop() + + +if __name__ == '__main__': + try: + main() + except KeyboardInterrupt: + pass + diff --git a/gr-cvsd-vocoder/Makefile.am b/gr-cvsd-vocoder/Makefile.am new file mode 100644 index 00000000..d6c03e1e --- /dev/null +++ b/gr-cvsd-vocoder/Makefile.am @@ -0,0 +1,25 @@ +# +# Copyright 2004 Free Software Foundation, Inc. +# +# This file is part of GNU Radio +# +# GNU Radio 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 2, or (at your option) +# any later version. +# +# GNU Radio 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 GNU Radio; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, +# Boston, MA 02110-1301, USA. +# + +include $(top_srcdir)/Makefile.common + +SUBDIRS = src +DIST_SUBDIRS = src diff --git a/gr-cvsd-vocoder/src/Makefile.am b/gr-cvsd-vocoder/src/Makefile.am new file mode 100644 index 00000000..79b21729 --- /dev/null +++ b/gr-cvsd-vocoder/src/Makefile.am @@ -0,0 +1,22 @@ +# +# Copyright 2004 Free Software Foundation, Inc. +# +# This file is part of GNU Radio +# +# GNU Radio 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 2, or (at your option) +# any later version. +# +# GNU Radio 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 GNU Radio; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, +# Boston, MA 02110-1301, USA. +# + +SUBDIRS = lib python diff --git a/gr-cvsd-vocoder/src/lib/Makefile.am b/gr-cvsd-vocoder/src/lib/Makefile.am new file mode 100644 index 00000000..644966b8 --- /dev/null +++ b/gr-cvsd-vocoder/src/lib/Makefile.am @@ -0,0 +1,97 @@ +# +# Copyright 2004,2005 Free Software Foundation, Inc. +# +# This file is part of GNU Radio +# +# GNU Radio 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 2, or (at your option) +# any later version. +# +# GNU Radio 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 GNU Radio; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, +# Boston, MA 02110-1301, USA. +# + +include $(top_srcdir)/Makefile.common + +# Install this stuff so that it ends up as the gnuradio.vocoder module +# This usually ends up at: +# ${prefix}/lib/python${python_version}/site-packages/gnuradio/vocoder + +ourpythondir = $(grpythondir)/vocoder +ourlibdir = $(grpyexecdir)/vocoder + +INCLUDES = $(STD_DEFINES_AND_INCLUDES) $(PYTHON_CPPFLAGS) + +SWIGPYTHONARGS = $(SWIGPYTHONFLAGS) $(STD_DEFINES_AND_INCLUDES) + + +ALL_IFILES = \ + $(LOCAL_IFILES) \ + $(NON_LOCAL_IFILES) + +NON_LOCAL_IFILES = \ + $(GNURADIO_I) + + +LOCAL_IFILES = \ + $(top_srcdir)/gr-cvsd-vocoder/src/lib/cvsd_vocoder.i + +# These files are built by SWIG. The first is the C++ glue. +# The second is the python wrapper that loads the _howto shared library +# and knows how to call our extensions. + +BUILT_SOURCES = \ + cvsd_vocoder.cc \ + cvsd_vocoder.py + +# This gets cvsd_vocoder.py installed in the right place +ourpython_PYTHON = \ + __init__.py \ + cvsd_vocoder.py + +ourlib_LTLIBRARIES = _cvsd_vocoder.la + +# These are the source files that go into the shared library +_cvsd_vocoder_la_SOURCES = \ + cvsd_decode_bs.cc \ + cvsd_encode_sb.cc \ + cvsd_vocoder.cc + + +# magic flags +_cvsd_vocoder_la_LDFLAGS = $(NO_UNDEFINED) -module -avoid-version + +# link the library against the c++ standard library +_cvsd_vocoder_la_LIBADD = \ + $(PYTHON_LDFLAGS) \ + $(GNURADIO_CORE_LA) \ + -lstdc++ + +cvsd_vocoder.cc cvsd_vocoder.py: cvsd_vocoder.i $(ALL_IFILES) + $(SWIG) $(SWIGPYTHONARGS) -module cvsd_vocoder -o cvsd_vocoder.cc $(LOCAL_IFILES) + +# These headers get installed in ${prefix}/include/gnuradio +grinclude_HEADERS = \ + cvsd_decode_bs.h \ + cvsd_encode_sb.h + +# These swig headers get installed in ${prefix}/include/gnuradio/swig +swiginclude_HEADERS = \ + $(LOCAL_IFILES) + + +MOSTLYCLEANFILES = $(BUILT_SOURCES) *.pyc + +# Don't distribute output of swig +dist-hook: + @for file in $(BUILT_SOURCES); do echo $(RM) $(distdir)/$$file; done + @for file in $(BUILT_SOURCES); do $(RM) $(distdir)/$$file; done + diff --git a/gr-cvsd-vocoder/src/lib/__init__.py b/gr-cvsd-vocoder/src/lib/__init__.py new file mode 100644 index 00000000..4f066df0 --- /dev/null +++ b/gr-cvsd-vocoder/src/lib/__init__.py @@ -0,0 +1,20 @@ +# +# Copyright 2005 Free Software Foundation, Inc. +# +# This file is part of GNU Radio +# +# GNU Radio 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 2, or (at your option) +# any later version. +# +# GNU Radio 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 GNU Radio; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, +# Boston, MA 02110-1301, USA. +# diff --git a/gr-cvsd-vocoder/src/lib/cvsd_decode_bs.cc b/gr-cvsd-vocoder/src/lib/cvsd_decode_bs.cc new file mode 100644 index 00000000..def2d946 --- /dev/null +++ b/gr-cvsd-vocoder/src/lib/cvsd_decode_bs.cc @@ -0,0 +1,196 @@ +/* -*- c++ -*- */ +/* + * Copyright 2007 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio 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 2, or (at your option) + * any later version. + * + * GNU Radio 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 GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +/* + * config.h is generated by configure. It contains the results + * of probing for features, options etc. It should be the first + * file included in your .cc file. + */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include + +/* + * Create a new instance of cvsd_decode_bs and return + * a boost shared_ptr. This is effectively the public constructor. + */ +cvsd_decode_bs_sptr +cvsd_make_decode_bs (short min_step, short max_step, double step_decay, + double accum_decay, int K, int J, + short pos_accum_max, short neg_accum_max) +{ + return cvsd_decode_bs_sptr (new cvsd_decode_bs (min_step, max_step, + step_decay, accum_decay, K, J, + pos_accum_max, neg_accum_max)); +} + +cvsd_decode_bs::cvsd_decode_bs (short min_step, short max_step, double step_decay, + double accum_decay, int K, int J, + short pos_accum_max, short neg_accum_max) + : gr_sync_interpolator ("cvsd_decode_bs", + gr_make_io_signature (1, 1, sizeof (unsigned char)), + gr_make_io_signature (1, 1, sizeof (short)), + 8), + d_min_step (min_step), d_max_step(max_step), d_step_decay(step_decay), + d_accum_decay(accum_decay), d_K(K), d_J(J), + d_pos_accum_max(pos_accum_max), d_neg_accum_max(neg_accum_max), + d_accum(0), + d_loop_counter(1), + d_runner(0), + d_runner_mask(0), + d_stepsize(min_step) + +{ + assert(d_pos_accum_max <= SHRT_MAX); + assert(d_neg_accum_max >= -SHRT_MAX); + assert(d_K <= 32); + assert(d_J <= d_K); + + // nothing else required in this example +} + + +cvsd_decode_bs::~cvsd_decode_bs () +{ + // nothing else required in this example +} + +unsigned char cvsd_decode_bs::cvsd_bitwise_sum (unsigned int input) +{ + unsigned int temp=input; + unsigned char bits=0; + + while(temp) { + temp=temp&(temp-1); + bits++; + } + return bits; +} + +int cvsd_decode_bs::cvsd_round (double input) +{ + double temp; + temp=input+0.5; + temp=floor(temp); + + return (int)temp; +} + +unsigned int cvsd_decode_bs::cvsd_pow (short radix, short power) +{ + double d_radix = (double) radix; + int i_power = (int) power; + double output; + + output=pow(d_radix,i_power); + return ( (unsigned int) cvsd_round(output)); +} + + +int +cvsd_decode_bs::work (int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) +{ + + + const unsigned char *in = (const unsigned char *) input_items[0]; + short *out = (short *) output_items[0]; + + int i=0; + short output_short=0; // 2 bytes 0 .. 65,535 + unsigned char bit_count=0; // 1 byte, 0 .. 255 + unsigned int mask=0; // 4 bytes, 0 .. 4,294,967,295 + unsigned char input_byte=0; // 1 bytes + unsigned char input_bit=0; // 1 byte, 0 .. 255 + + // Loop through each input data point + for(i = 0; i < noutput_items/8.0; i++) { + + input_byte = in[i]; + // Initiliaze bit counter + bit_count=0; + + while(bit_count<8) { + // Compute the Appropriate Mask + mask=cvsd_pow(2,7-bit_count); + + // Pull off the corresponding bit + input_bit = input_byte & mask; + + // Update the bit counter + bit_count++; + + // Update runner with the next input bit + // Runner is a shift-register; shift left, add on newest output bit + d_runner = (d_runner<<1) | ((unsigned int) input_bit); + + // Run this only if you have >= J bits in your shift register + if (d_loop_counter>=d_J) { + // Update Step Size + d_runner_mask=(cvsd_pow(2,d_J)-1); + if ((cvsd_bitwise_sum(d_runner & d_runner_mask)>=d_J)||(cvsd_bitwise_sum((~d_runner) & d_runner_mask)>=d_J)) { + // Runs of 1s and 0s + d_stepsize = std::min( (short) (d_stepsize + d_min_step), d_max_step); + } + else { + // No runs of 1s and 0s + d_stepsize = std::max( (short) cvsd_round(d_stepsize*d_step_decay), d_min_step); + } + } + + // Update Accum (i.e. the reference value) + if (input_bit) { + d_accum=d_accum+d_stepsize; + } + else { + d_accum=d_accum-d_stepsize; + } + + // Multiply by Accum_Decay + d_accum=(cvsd_round(d_accum*d_accum_decay)); + + // Check for overflow + if (d_accum >=((int) d_pos_accum_max)) { + d_accum=(int)d_pos_accum_max; + } + else if (d_accum <=((int) d_neg_accum_max)) { + d_accum=(int)d_neg_accum_max; + } + + // Find the output short to write to the file + output_short=((short) d_accum); + + if (d_loop_counter <= d_K) { + d_loop_counter++; + } + + *(out++) = output_short; + } // while () + + } // for() + + return noutput_items; +} diff --git a/gr-cvsd-vocoder/src/lib/cvsd_decode_bs.h b/gr-cvsd-vocoder/src/lib/cvsd_decode_bs.h new file mode 100644 index 00000000..d23c20e3 --- /dev/null +++ b/gr-cvsd-vocoder/src/lib/cvsd_decode_bs.h @@ -0,0 +1,97 @@ +/* -*- c++ -*- */ +/* + * Copyright 2007 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio 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 2, or (at your option) + * any later version. + * + * GNU Radio 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 GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ +#ifndef INCLUDED_CVSD_DECODE_BS_H +#define INCLUDED_CVSD_DECODE_BS_H + +#include + +class cvsd_decode_bs; + +typedef boost::shared_ptr cvsd_decode_bs_sptr; + +cvsd_decode_bs_sptr cvsd_make_decode_bs (short min_step=10, + short max_step=1280, + double step_decay=0.9990234375, + double accum_decay= 0.96875, + int K=32, + int J=4, + short pos_accum_max=32767, + short neg_accum_max=-32767); + +class cvsd_decode_bs : public gr_sync_interpolator +{ +private: + friend cvsd_decode_bs_sptr cvsd_make_decode_bs (short min_step, + short max_step, + double step_decay, + double accum_decay, + int K, + int J, + short pos_accum_max, + short neg_accum_max); + + cvsd_decode_bs (short min_step, short max_step, double step_decay, + double accum_decay, int K, int J, + short pos_accum_max, short neg_accum_max); + + int cvsd_round(double input); + unsigned int cvsd_pow (short radix, short power); + unsigned char cvsd_bitwise_sum (unsigned int input); + + short d_min_step; + short d_max_step; + double d_step_decay; + double d_accum_decay; + + //! \brief Size of shift register; the number of output bits remembered in shift register + int d_K; + + //! \brief Number of bits in the shift register that are equal; size of run of 1s, 0s + int d_J; + + short d_pos_accum_max; + short d_neg_accum_max; + + int d_accum; + int d_loop_counter; + unsigned int d_runner; + unsigned int d_runner_mask; // 4 bytes, 0 .. 4,294,967,295 + short d_stepsize; + + public: + ~cvsd_decode_bs (); // public destructor + + short min_step() { return d_min_step; } + short max_step() { return d_max_step; } + double step_decay() { return d_step_decay; } + double accum_decay() { return d_accum_decay; } + int K() { return d_K; } + int J() { return d_J; } + short pos_accum_max() { return d_pos_accum_max; } + short neg_accum_max() { return d_neg_accum_max; } + + int work (int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); +}; + +#endif /* INCLUDED_CVSD_DECODE_BS_H */ diff --git a/gr-cvsd-vocoder/src/lib/cvsd_encode_sb.cc b/gr-cvsd-vocoder/src/lib/cvsd_encode_sb.cc new file mode 100644 index 00000000..37e2fb0b --- /dev/null +++ b/gr-cvsd-vocoder/src/lib/cvsd_encode_sb.cc @@ -0,0 +1,194 @@ +/* -*- c++ -*- */ +/* + * Copyright 2007 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio 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 2, or (at your option) + * any later version. + * + * GNU Radio 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 GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +/* + * config.h is generated by configure. It contains the results + * of probing for features, options etc. It should be the first + * file included in your .cc file. + */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include + +/* + * Create a new instance of cvsd_encode_sb and return + * a boost shared_ptr. This is effectively the public constructor. + */ +cvsd_encode_sb_sptr +cvsd_make_encode_sb (short min_step, short max_step, double step_decay, + double accum_decay, int K, int J, + short pos_accum_max, short neg_accum_max) +{ + return cvsd_encode_sb_sptr (new cvsd_encode_sb (min_step, max_step, + step_decay, accum_decay, K, J, + pos_accum_max, neg_accum_max)); +} + +cvsd_encode_sb::cvsd_encode_sb (short min_step, short max_step, double step_decay, + double accum_decay, int K, int J, + short pos_accum_max, short neg_accum_max) + : gr_sync_decimator ("cvsd_encode_sb", + gr_make_io_signature (1, 1, sizeof (short)), + gr_make_io_signature (1, 1, sizeof (unsigned char)), + 8), + d_min_step (min_step), d_max_step(max_step), d_step_decay(step_decay), + d_accum_decay(accum_decay), d_K(K), d_J(J), + d_pos_accum_max(pos_accum_max), d_neg_accum_max(neg_accum_max), + d_accum(0), + d_loop_counter(1), + d_runner(0), + d_stepsize(min_step) + +{ + assert(d_pos_accum_max <= SHRT_MAX); + assert(d_neg_accum_max >= -SHRT_MAX); + assert(d_K <= 32); + assert(d_J <= d_K); + + // nothing else required in this example +} + + +cvsd_encode_sb::~cvsd_encode_sb () +{ + // nothing else required in this example +} + +unsigned char cvsd_encode_sb::cvsd_bitwise_sum (unsigned int input) +{ + unsigned int temp=input; + unsigned char bits=0; + + while(temp) { + temp=temp&(temp-1); + bits++; + } + return bits; +} + +int cvsd_encode_sb::cvsd_round (double input) +{ + double temp; + temp=input+0.5; + temp=floor(temp); + + return (int)temp; +} + +unsigned int cvsd_encode_sb::cvsd_pow (short radix, short power) +{ + double d_radix = (double) radix; + int i_power = (int) power; + double output; + + output=pow(d_radix,i_power); + return ( (unsigned int) cvsd_round(output)); +} + + + +int +cvsd_encode_sb::work (int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) +{ + const short *in = (const short *) input_items[0]; + unsigned char *out = (unsigned char *) output_items[0]; + + unsigned short i=0; // 2 bytes, 0 .. 65,535 + unsigned char output_bit=0; // 1 byte, 0 .. 255 + unsigned char output_byte=0; // 1 bytes 0.255 + unsigned char bit_count=0; // 1 byte, 0 .. 255 + unsigned int mask=0; // 4 bytes, 0 .. 4,294,967,295 + + // Loop through each input data point + for(i = 0; i < noutput_items*8; i++) { + if((int)in[i] >= d_accum) { // Note: sign((data(n)-accum)) + output_bit=1; + } + else { + output_bit=0; + } + + // Update Accum (i.e. the reference value) + if (output_bit) { + d_accum=d_accum+d_stepsize; + //printf("Addding %d to the accum; the result is: %d.\n", d_stepsize, d_accum); + } + else { + d_accum=d_accum-d_stepsize; + //printf("Subtracting %d to the accum; the result is: %d.\n", d_stepsize, d_accum); + } + + // Multiply by Accum_Decay + d_accum=(cvsd_round(d_accum*d_accum_decay)); + + // Check for overflow + if (d_accum >= ((int)d_pos_accum_max)) { + d_accum = (int)d_pos_accum_max; + } + else if(d_accum <= ((int) d_neg_accum_max)) { + d_accum = (int) d_neg_accum_max; + } + + // Update runner with the last output bit + // Update Step Size + if (d_loop_counter >= d_J) { // Run this only if you have >= J bits in your shift register + mask=(cvsd_pow(2, d_J) - 1); + if ((cvsd_bitwise_sum(d_runner & mask) >= d_J) || (cvsd_bitwise_sum((~d_runner) & mask) >= d_J)) { + // Runs of 1s and 0s + d_stepsize = std::min( (short)(d_stepsize + d_min_step), d_max_step); + } + else { + // No runs of 1s and 0s + d_stepsize = std::max( (short)cvsd_round(d_stepsize*d_step_decay), d_min_step); + } + } + + // Runner is a shift-register; shift left, add on newest output bit + d_runner = (d_runner<<1) | ((unsigned int) output_bit); + + // Update the ouput type; shift left, add on newest output bit + // If you have put in 8 bits, output it as a byte + output_byte = (output_byte<<1) | output_bit; + bit_count++; + + if (d_loop_counter <= d_K) { + d_loop_counter++; + } + + // If you have put 8 bits, output and clear. + if (bit_count==8) { + // Read in short from the file + *(out++) = output_byte; + + // Reset the bit_count + bit_count=0; + output_byte=0; + } + } // While + + return noutput_items; +} diff --git a/gr-cvsd-vocoder/src/lib/cvsd_encode_sb.h b/gr-cvsd-vocoder/src/lib/cvsd_encode_sb.h new file mode 100644 index 00000000..57734ca1 --- /dev/null +++ b/gr-cvsd-vocoder/src/lib/cvsd_encode_sb.h @@ -0,0 +1,96 @@ +/* -*- c++ -*- */ +/* + * Copyright 2007 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio 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 2, or (at your option) + * any later version. + * + * GNU Radio 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 GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ +#ifndef INCLUDED_CVSD_ENCODER_SB_H +#define INCLUDED_CVSD_ENCODER_SB_H + +#include + +class cvsd_encode_sb; + +typedef boost::shared_ptr cvsd_encode_sb_sptr; + +cvsd_encode_sb_sptr cvsd_make_encode_sb (short min_step=10, + short max_step=1280, + double step_decay=0.9990234375, + double accum_decay= 0.96875, + int K=32, + int J=4, + short pos_accum_max=32767, + short neg_accum_max=-32767); + +class cvsd_encode_sb : public gr_sync_decimator +{ +private: + friend cvsd_encode_sb_sptr cvsd_make_encode_sb (short min_step, + short max_step, + double step_decay, + double accum_decay, + int K, + int J, + short pos_accum_max, + short neg_accum_max); + + cvsd_encode_sb (short min_step, short max_step, double step_decay, + double accum_decay, int K, int J, + short pos_accum_max, short neg_accum_max); + + int cvsd_round(double input); + unsigned int cvsd_pow (short radix, short power); + unsigned char cvsd_bitwise_sum (unsigned int input); + + short d_min_step; + short d_max_step; + double d_step_decay; + double d_accum_decay; + + //! \brief Size of shift register; the number of output bits remembered in shift register + int d_K; + + //! \brief Number of bits in the shift register that are equal; size of run of 1s, 0s + int d_J; + + short d_pos_accum_max; + short d_neg_accum_max; + + int d_accum; + int d_loop_counter; + unsigned int d_runner; + short d_stepsize; + + public: + ~cvsd_encode_sb (); // public destructor + + short min_step() { return d_min_step; } + short max_step() { return d_max_step; } + double step_decay() { return d_step_decay; } + double accum_decay() { return d_accum_decay; } + int K() { return d_K; } + int J() { return d_J; } + short pos_accum_max() { return d_pos_accum_max; } + short neg_accum_max() { return d_neg_accum_max; } + + int work (int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); +}; + +#endif /* INCLUDED_CVSD_ENCODE_SB_H */ diff --git a/gr-cvsd-vocoder/src/lib/cvsd_vocoder.i b/gr-cvsd-vocoder/src/lib/cvsd_vocoder.i new file mode 100644 index 00000000..5e618187 --- /dev/null +++ b/gr-cvsd-vocoder/src/lib/cvsd_vocoder.i @@ -0,0 +1,85 @@ +/* -*- c++ -*- */ +/* + * Copyright 2007 Free Software Foundation, Inc. + * + * This file is part of GNU Radio + * + * GNU Radio 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 2, or (at your option) + * any later version. + * + * GNU Radio 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 GNU Radio; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +%feature("autodoc","1"); +%include "exception.i" +%import "gnuradio.i" + +%{ +#include "gnuradio_swig_bug_workaround.h" // mandatory bug fix +#include "cvsd_encode_sb.h" +#include "cvsd_decode_bs.h" +#include +%} + +GR_SWIG_BLOCK_MAGIC(cvsd,encode_sb); + +cvsd_encode_sb_sptr cvsd_make_encode_sb (short min_step=10, + short max_step=1280, + double step_decay=0.9990234375, + double accum_decay= 0.96875, + int K=32, + int J=4, + short pos_accum_max=32767, + short neg_accum_max=-32767); + +class cvsd_encode_sb : public gr_sync_decimator +{ +private: + cvsd_encode_sb (short min_step, short max_step, double step_decay, + double accum_decay, int K, int J, + short pos_accum_max, short neg_accum_max); + + + public: + short min_step() { return d_min_step; } + short max_step() { return d_max_step; } + double step_decay() { return d_step_decay; } + double accum_decay() { return d_accum_decay; } + int K() { return d_K; } + int J() { return d_J; } + short pos_accum_max() { return d_pos_accum_max; } + short neg_accum_max() { return d_neg_accum_max; } +}; + +// ---------------------------------------------------------------- + +GR_SWIG_BLOCK_MAGIC(cvsd,decode_bs); + +cvsd_decode_bs_sptr cvsd_make_decode_bs (short min_step=10, + short max_step=1280, + double step_decay=0.9990234375, + double accum_decay= 0.96875, + int K=32, + int J=4, + short pos_accum_max=32767, + short neg_accum_max=-32767); + +class cvsd_decode_bs : public gr_sync_interpolator +{ +private: + cvsd_decode_bs (short min_step, short max_step, double step_decay, + double accum_decay, int K, int J, + short pos_accum_max, short neg_accum_max); + + public: +}; diff --git a/gr-cvsd-vocoder/src/python/Makefile.am b/gr-cvsd-vocoder/src/python/Makefile.am new file mode 100644 index 00000000..ce2938c3 --- /dev/null +++ b/gr-cvsd-vocoder/src/python/Makefile.am @@ -0,0 +1,40 @@ +# +# Copyright 2004 Free Software Foundation, Inc. +# +# This file is part of GNU Radio +# +# GNU Radio 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 2, or (at your option) +# any later version. +# +# GNU Radio 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 GNU Radio; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, +# Boston, MA 02110-1301, USA. +# + +include $(top_srcdir)/Makefile.common + +EXTRA_DIST = run_tests.in + + +TESTS = \ + run_tests + + +grblkspythondir = $(grpythondir)/blksimpl + +grblkspython_PYTHON = \ + cvsd.py + +noinst_PYTHON = \ + encdec.py \ + qa_cvsd_vocoder.py + +CLEANFILES = *.pyc *.pyo diff --git a/gr-cvsd-vocoder/src/python/cvsd.py b/gr-cvsd-vocoder/src/python/cvsd.py new file mode 100644 index 00000000..dcb6bb50 --- /dev/null +++ b/gr-cvsd-vocoder/src/python/cvsd.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python +# +# Copyright 2007 Free Software Foundation, Inc. +# +# This file is part of GNU Radio +# +# GNU Radio 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 2, or (at your option) +# any later version. +# +# GNU Radio 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 GNU Radio; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, +# Boston, MA 02110-1301, USA. +# + +from gnuradio import gr +from gnuradio.vocoder import cvsd_vocoder + +class cvsd_encode(gr.hier_block): + def __init__(self, fg, resample=8): + scale_factor = 32000.0 + self.interp = resample + + src_scale = gr.multiply_const_ff(scale_factor) + interp = gr.interp_fir_filter_fff(self.interp, self.interp*(1,)) + f2s = gr.float_to_short() + enc = cvsd_vocoder.encode_sb() + + fg.connect(src_scale, interp, f2s, enc) + gr.hier_block.__init__(self, fg, src_scale, enc) + + +class cvsd_decode(gr.hier_block): + def __init__(self, fg, resample=8): + scale_factor = 32000.0 + self.decim = resample + + dec = cvsd_vocoder.decode_bs() + s2f = gr.short_to_float() + decim = gr.fir_filter_fff(self.decim, (1,)) + sink_scale = gr.multiply_const_ff(1.0/scale_factor) + + fg.connect(dec, s2f, decim, sink_scale) + gr.hier_block.__init__(self, fg, dec, sink_scale) + diff --git a/gr-cvsd-vocoder/src/python/encdec.py b/gr-cvsd-vocoder/src/python/encdec.py new file mode 100755 index 00000000..b0afe5eb --- /dev/null +++ b/gr-cvsd-vocoder/src/python/encdec.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python +# +# Copyright 2007 Free Software Foundation, Inc. +# +# This file is part of GNU Radio +# +# GNU Radio 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 2, or (at your option) +# any later version. +# +# GNU Radio 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 GNU Radio; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, +# Boston, MA 02110-1301, USA. +# + +from gnuradio import gr, blks +from gnuradio import audio +from gnuradio.vocoder import cvsd_vocoder + +def build_graph(): + sample_rate = 8000 + scale_factor = 32000 + + fg = gr.flow_graph() + src = audio.source(sample_rate, "plughw:0,0") + src_scale = gr.multiply_const_ff(scale_factor) + + interp = blks.rational_resampler_fff(fg, 8, 1) + f2s = gr.float_to_short () + + enc = cvsd_vocoder.encode_sb() + dec = cvsd_vocoder.decode_bs() + + s2f = gr.short_to_float () + decim = blks.rational_resampler_fff(fg, 1, 8) + + sink_scale = gr.multiply_const_ff(1.0/scale_factor) + sink = audio.sink(sample_rate, "plughw:0,0") + + fg.connect(src, src_scale, interp, f2s, enc) + fg.connect(enc, dec, s2f, decim, sink_scale, sink) + + if 0: # debug + fg.connect(src, gr.file_sink(gr.sizeof_float, "source.dat")) + fg.connect(src_scale, gr.file_sink(gr.sizeof_float, "src_scale.dat")) + fg.connect(interp, gr.file_sink(gr.sizeof_float, "interp.dat")) + fg.connect(f2s, gr.file_sink(gr.sizeof_short, "f2s.dat")) + fg.connect(enc, gr.file_sink(gr.sizeof_char, "enc.dat")) + fg.connect(dec, gr.file_sink(gr.sizeof_short, "dec.dat")) + fg.connect(s2f, gr.file_sink(gr.sizeof_float, "s2f.dat")) + fg.connect(decim, gr.file_sink(gr.sizeof_float, "decim.dat")) + fg.connect(sink_scale, gr.file_sink(gr.sizeof_float, "sink_scale.dat")) + + return fg + +if __name__ == '__main__': + fg = build_graph() + fg.start() + raw_input ('Press Enter to exit: ') + fg.stop() diff --git a/gr-cvsd-vocoder/src/python/qa_cvsd_vocoder.py b/gr-cvsd-vocoder/src/python/qa_cvsd_vocoder.py new file mode 100755 index 00000000..d20b5eef --- /dev/null +++ b/gr-cvsd-vocoder/src/python/qa_cvsd_vocoder.py @@ -0,0 +1,112 @@ +#!/usr/bin/env python +# +# Copyright 2007 Free Software Foundation, Inc. +# +# This file is part of GNU Radio +# +# GNU Radio 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 2, or (at your option) +# any later version. +# +# GNU Radio 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 GNU Radio; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, +# Boston, MA 02110-1301, USA. +# + +from gnuradio import gr, gr_unittest, blks +import cvsd_vocoder + +class qa_cvsd_test (gr_unittest.TestCase): + + def setUp (self): + self.fg = gr.flow_graph () + + def tearDown (self): + self.fg = None + + def test01(self): + sample_rate = 8000 + scale_factor = 32000 + + expected_data = (6.9670547250243192e-21, -2.4088578356895596e-05, + -5.1261918997624889e-05, 7.2410854045301676e-05, + 8.444241393590346e-05, -1.2537107068055775e-05, + 0.00024186755763366818, -0.00060463894624263048, + 0.00064864184241741896, 0.010409165173768997, + 0.0087582804262638092, 0.017965050414204597, + 0.010722399689257145, 0.006602009292691946, + 0.02213001623749733, 0.0079685859382152557, + 0.033707316964864731, 0.027021972462534904, + 0.0086071854457259178, 0.0081678871065378189, + 0.039343506097793579, 0.030671956017613411, + 0.029626710340380669, 0.020126519724726677, + 0.023636780679225922, 0.0064640454947948456, + -0.0038861562497913837, 0.0021134600974619389, + -0.0088051930069923401, -0.00023228264763019979, + -0.033737499266862869, -0.033141419291496277, + -0.037145044654607773, -0.0080892946571111679, + -0.077117636799812317, -0.078382067382335663, + -0.055503919720649719, -0.019355267286300659, + -0.022441385313868523, -0.073706060647964478, + -0.054677654057741165, -0.047119375318288803, + -0.044418536126613617, -0.036084383726119995, + -0.0206278245896101, -0.031200021505355835, + -0.0004070434661116451, 0.0006594572332687676, + -0.016584658995270729, 0.07387717068195343, + -0.0063191778026521206, 0.051200628280639648, + -0.029480356723070145, 0.05176771804690361, + 0.038578659296035767, 0.026550088077783585, + 0.067103870213031769, 0.001888439292088151, + 0.28141644597053528, 0.49543789029121399, + 0.6626054048538208, 0.79180729389190674, + 0.89210402965545654, 0.96999943256378174, + 1.0261462926864624, 1.0267977714538574, + 1.0251555442810059, 1.0265737771987915, + 1.0278496742248535, 1.0208886861801147, + 1.0325057506561279, 0.91415292024612427, + 0.83941859006881714, 0.67373806238174438, + 0.51683622598648071, 0.38949671387672424, + 0.16016888618469238, 0.049505095928907394, + -0.16699212789535522, -0.26886492967605591, + -0.49256673455238342, -0.59178370237350464, + -0.73317724466323853, -0.78922677040100098, + -0.88782668113708496, -0.96708977222442627, + -0.96490746736526489, -0.94962418079376221, + -0.94716215133666992, -0.93755108118057251, + -0.84852480888366699, -0.80485564470291138, + -0.69762390851974487, -0.58398681879043579, + -0.45891636610031128, -0.29681697487831116, + -0.16035343706607819, 0.014823081903159618, + 0.16282452642917633, 0.33802291750907898) + + src = gr.sig_source_f(sample_rate, gr.GR_SIN_WAVE, 200, 1, 0) + head = gr.head(gr.sizeof_float, 100) + src_scale = gr.multiply_const_ff(scale_factor) + + interp = blks.rational_resampler_fff(self.fg, 8, 1) + f2s = gr.float_to_short () + + enc = cvsd_vocoder.encode_sb() + dec = cvsd_vocoder.decode_bs() + + s2f = gr.short_to_float () + decim = blks.rational_resampler_fff(self.fg, 1, 8) + + sink_scale = gr.multiply_const_ff(1.0/scale_factor) + sink = gr.vector_sink_f() + + self.fg.connect(src, head, src_scale, interp, f2s, enc) + self.fg.connect(enc, dec, s2f, decim, sink_scale, sink) + self.fg.run() + + self.assertFloatTuplesAlmostEqual (expected_data, sink.data(), 5) + +if __name__ == '__main__': + gr_unittest.main () diff --git a/gr-cvsd-vocoder/src/python/run_tests.in b/gr-cvsd-vocoder/src/python/run_tests.in new file mode 100644 index 00000000..be969e28 --- /dev/null +++ b/gr-cvsd-vocoder/src/python/run_tests.in @@ -0,0 +1,10 @@ +#!/bin/sh + +# 1st parameter is absolute path to component source directory +# 2nd parameter is absolute path to component build directory +# 3rd parameter is path to Python QA directory + +@top_builddir@/run_tests.sh \ + @abs_top_srcdir@/gr-cvsd-vocoder \ + @abs_top_builddir@/gr-cvsd-vocoder \ + @srcdir@ -- 2.30.2