Source

dsptools / lib / dsptools / fir.py

Full commit
#
# Copyright (c) 2011, Davide Cittaro
# 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 name of the <organization> nor the
#       names of its 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 <COPYRIGHT HOLDER> 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.
#

import tables
import numpy as np
import scipy.signal as ssig

# Functions

def nextpow2(x):
  return np.ceil(np.log2(np.abs(x)))


def pad_zero(data, padded, pos = 1):
  # ok, this is tough... we have to pad signall that is written on disk
  # better way to do it? Mmm, I would say we write a temporary copy
  
  l_data = len(data)
  l_padded = len(padded)
  pad_array = np.zeros(l_padded / 2 - l_data / 2)
  npad = len(pad_array)
  if pos == 0:
    padded[:2*npad] = np.concatenate([pad_array, pad_array])
    padded[2 * npad:] = data
  elif pos = 1:
    padded[:npad] = pad_array
    padded[npad:-npad] = data
    padded[-npad:] = pad_array
  elif pos = 2:
    padded[:-2 * npad] = data
    padded[-2 * npad:] = np.concatenate([pad_array, pad_array])

# Classes


class __fir__():
  def __init__(self, fir_size):
    self._FIR = np.array([])
  def smooth(self, signal):
    # pad signal
    l_padded = len(signal) + len(self._FIR)

    fname = 'changeme'
    filters = tables.Filters(complevel=5, complib='blosc')

    padded_signal = tables.openFile(fname, mode = 'w', filters = filters)
    padded_signal.createCArray("/", "pad", tables.Float64Atom(), (l_padded,))
    padded_signal = pad_zero(signal, padded_signal)
    
  


class hanning(__fir__):
  def __init__(self, fir_size):
    self._FIR = ssig.hanning(fir_size)

class hamming(__fir__):
  def __init__(self, fir_size):
    self._FIR = ssig.hamming(fir_size)

class blackman(__fir__):
  def __init__(self, fir_size):
    self._FIR = ssig.blackman(fir_size)

class bartlett(__fir__):
  def __init__(self, fir_size):
    self._FIR = ssig.bartlett(fir_size)

class flattop(__fir__):
  def __init__(self, fir_size):
    self._FIR = ssig.flattop(fir_size)

class nuttall(__fir__):
  def __init__(self, fir_size):
    self._FIR = ssig.nuttall(fir_size)
    
class morlet(__fir__):
  def __init__(self, fir_size):
    self._FIR = ssig.morlet(fir_size)

class chebwin(__fir__):
  def __init__(self, fir_size, dB):
    self._FIR = ssig.chebwin(fir_size, dB)

class flat(__fir__):
  def __init__(self, fir_size):
    self._FIR = np.ones(fir_size, 'd')

class gaussian(__fir__):
  def __init__(self, fir_size, std_dev):
    self._FIR = np.ssig.gaussian(fir_size, std_dev)