#include "block.h" #include "fir.h" #include "window.h" #include "silowpass.h" class FIRLowpassHamming : public Block { private: InPort < double > * in; OutPort < double > * out; Parameter < long > * samples; Parameter < double > * fc; Fir < double > fir; public: FIRLowpassHamming(const Blockopt & blockopt) : Block(blockopt) {} void makeParameters() { samples = new Parameter < long > (this, "samples", 10); fc = new Parameter < double > (this, "fc", 0.5); } void makeIOPorts() { in = new InPort < double > (this, "in", 1); out = new OutPort < double > (this, "out", 1); } void initialize() { long fulllength = samples->value(); long halflength = fulllength / 2; double firprototype[fulllength]; if ( halflength*2 == fulllength ) { // even no.of samples ( group delay is NOT an integer ) for (long k = 0; k < halflength; k++) { firprototype[halflength + k] = firprototype[halflength - 1 - k] = siLowpass(double(k + 0.5), fc->value()); } } else { // odd no.of samples ( group delay IS an integer ) for (long k = 0; k <= halflength; k++) { firprototype[halflength + k] = firprototype[halflength - k] = siLowpass(double(k), fc->value()); } } // multiply with Hamming window for (long k = 0;k < fulllength;k++) { firprototype[k] *= hamming(fulllength, k); } // normalize to 0dB at frequency 0 double sum = 0.; for (long k = 0; k < fulllength; k++) sum += firprototype[k]; for (long k = 0; k < fulllength; k++) firprototype[k] /= sum; fir.create(fulllength, firprototype); } void go() { out->write(fir.filter(in->read())); } }; PUBLISH(FIRLowpassHamming);