Skip to content

Commit

Permalink
Didn't like that phi
Browse files Browse the repository at this point in the history
  • Loading branch information
JayKickliter committed Sep 5, 2014
1 parent 9c215c5 commit 3b284aa
Showing 1 changed file with 47 additions and 47 deletions.
94 changes: 47 additions & 47 deletions src/Filters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ end
type FIRInterpolator <: FIRKernel
pfb::PFB
interpolation::Int
::Int
tapsPerΦ::Int
N𝜙::Int
tapsPer𝜙::Int
end

function FIRInterpolator( h::Vector, interpolation::Integer )
pfb = flipud( polyize( h, interpolation ) )
( tapsPerΦ, NΦ ) = size( pfb )
FIRInterpolator( pfb, interpolation, NΦ, tapsPerΦ )
( tapsPer𝜙, N𝜙 ) = size( pfb )
FIRInterpolator( pfb, interpolation, N𝜙, tapsPer𝜙 )
end


Expand All @@ -56,19 +56,19 @@ end
type FIRRational <: FIRKernel
pfb::PFB
ratio::Rational{Int}
::Int
tapsPerΦ::Int
N𝜙::Int
tapsPer𝜙::Int
criticalYidx::Int
ΦIdx::Int
𝜙Idx::Int
inputDeficit::Int
end

function FIRRational( h::Vector, ratio::Rational )
interpolation = num( ratio )
pfb = flipud( polyize( h, interpolation ) )
( tapsPerΦ, NΦ ) = size( pfb )
criticalYidx = ifloor( tapsPerΦ * ratio )
FIRRational( pfb, ratio, NΦ, tapsPerΦ, criticalYidx, 1, 1 )
( tapsPer𝜙, N𝜙 ) = size( pfb )
criticalYidx = ifloor( tapsPer𝜙 * ratio )
FIRRational( pfb, ratio, N𝜙, tapsPer𝜙, criticalYidx, 1, 1 )
end


Expand All @@ -92,10 +92,10 @@ function FIRFilter( h::Vector, resampleRatio::Rational = 1//1 )
reqDlyLineLen = kernel.hLen - 1
elseif decimation == 1 # interpolate
kernel = FIRInterpolator( h, interpolation )
reqDlyLineLen = kernel.tapsPerΦ - 1
reqDlyLineLen = kernel.tapsPer𝜙 - 1
else # rational
kernel = FIRRational( h, resampleRatio )
reqDlyLineLen = kernel.tapsPerΦ - 1
reqDlyLineLen = kernel.tapsPer𝜙 - 1
end

dlyLine = zeros( reqDlyLineLen )
Expand All @@ -117,8 +117,8 @@ end
# Does nothing for non-rational kernels
reset( self::FIRKernel ) = self

# For rational kernel, set ΦIdx back to 1
reset( self::FIRRational ) = self.ΦIdx = 1
# For rational kernel, set 𝜙Idx back to 1
reset( self::FIRRational ) = self.𝜙Idx = 1

# For FIRFilter, set delay line to zeros of same tyoe and required length
function reset( self::FIRFilter )
Expand Down Expand Up @@ -147,8 +147,8 @@ end

function polyize{T}( h::Vector{T}, numFilters::Integer )
hLen = length( h )
hLenPerΦ = iceil( hLen/numFilters )
pfbSize = hLenPerΦ * numFilters
hLenPer𝜙 = iceil( hLen/numFilters )
pfbSize = hLenPer𝜙 * numFilters

if hLen != pfbSize # check that the vector is an integer multiple of numFilters
hExtended = similar( h, pfbSize ) # No? extend and zero pad
Expand All @@ -158,8 +158,8 @@ function polyize{T}( h::Vector{T}, numFilters::Integer )
end

hLen = length( h )
hLenPerΦ = int( hLen/numFilters )
pfb = reshape( h, numFilters, hLenPerΦ )'
hLenPer𝜙 = int( hLen/numFilters )
pfb = reshape( h, numFilters, hLenPer𝜙 )'
end

#==============================================================================#
Expand All @@ -173,10 +173,10 @@ end
#
# ( It's hard to explain how this works without a diagram )

function outputlength( inputlength::Integer, ratio::Rational, initialΦ::Integer )
function outputlength( inputlength::Integer, ratio::Rational, initial𝜙::Integer )
interpolation = num( ratio )
decimation = den( ratio )
outLen = (( inputlength * interpolation ) - initialΦ + 1 ) / decimation
outLen = (( inputlength * interpolation ) - initial𝜙 + 1 ) / decimation
iceil( outLen )
end

Expand All @@ -196,7 +196,7 @@ end

function outputlength( self::FIRFilter{FIRRational}, inputlength::Integer )
kernel = self.kernel
outputlength( inputlength-kernel.inputDeficit+1, kernel.ratio, kernel.ΦIdx )
outputlength( inputlength-kernel.inputDeficit+1, kernel.ratio, kernel.𝜙Idx )
end


Expand All @@ -208,10 +208,10 @@ end
# | | \| | |__| | |___ |___ | \| #
#==============================================================================#

function inputlength( outputlength::Int, ratio::Rational, initialΦ::Integer )
function inputlength( outputlength::Int, ratio::Rational, initial𝜙::Integer )
interpolation = num( ratio )
decimation = den( ratio )
inLen = ( outputlength * decimation + initialΦ - 1 ) / interpolation
inLen = ( outputlength * decimation + initial𝜙 - 1 ) / interpolation
iceil( inLen )
end

Expand All @@ -232,7 +232,7 @@ end

function inputlength( self::FIRFilter{FIRRational}, outputlength::Integer )
kernel = self.kernel
inLen = inputlength( outputlength, kernel.ratio, kernel.ΦIdx )
inLen = inputlength( outputlength, kernel.ratio, kernel.𝜙Idx )
inLen = inLen + kernel.inputDeficit - 1
end

Expand All @@ -248,9 +248,9 @@ end
function nextphase( currentphase::Integer, ratio::Rational )
interpolation = num( ratio )
decimation = den( ratio )
ΦStep = mod( decimation, interpolation )
ΦNext = currentphase + ΦStep
ΦNext = ΦNext > interpolation ? ΦNext - interpolation : ΦNext
𝜙Step = mod( decimation, interpolation )
𝜙Next = currentphase + 𝜙Step
𝜙Next = 𝜙Next > interpolation ? 𝜙Next - interpolation : 𝜙Next
end


Expand Down Expand Up @@ -324,8 +324,8 @@ function filt!{T}( buffer::Vector{T}, self::FIRFilter{FIRInterpolator}, x::Vecto
pfb::PFB{T} = self.kernel.pfb
dlyLine::Vector{T} = self.dlyLine
interpolation = self.kernel.interpolation
= self.kernel.
tapsPerΦ = self.kernel.tapsPerΦ
N𝜙 = self.kernel.N𝜙
tapsPer𝜙 = self.kernel.tapsPer𝜙
xLen = length( x )
bufLen = length( buffer )
reqDlyLineLen = self.reqDlyLineLen
Expand All @@ -335,34 +335,34 @@ function filt!{T}( buffer::Vector{T}, self::FIRFilter{FIRInterpolator}, x::Vecto
bufLen >= outLen || error( "length( buffer ) must be >= interpolation * length(x)")

inputIdx = 1
Φ = 1
𝜙 = 1

for yIdx in 1:criticalYidx

accumulator = zero(T)

for k in 1:tapsPerΦ-inputIdx
@inbounds accumulator += pfb[k, Φ] * dlyLine[k+inputIdx-1]
for k in 1:tapsPer𝜙-inputIdx
@inbounds accumulator += pfb[k, 𝜙] * dlyLine[k+inputIdx-1]
end

for k in 1:inputIdx
@inbounds accumulator += pfb[tapsPerΦ-inputIdx+k, Φ] * x[k]
@inbounds accumulator += pfb[tapsPer𝜙-inputIdx+k, 𝜙] * x[k]
end

@inbounds buffer[yIdx] = accumulator
(Φ, inputIdx) = Φ == ? ( 1, inputIdx+1 ) : ( Φ+1, inputIdx )
(𝜙, inputIdx) = 𝜙 == N𝜙 ? ( 1, inputIdx+1 ) : ( 𝜙+1, inputIdx )
end

for yIdx in criticalYidx+1:outLen

accumulator = zero(T)

for k in 1:tapsPerΦ
@inbounds accumulator += pfb[ k, Φ ] * x[ inputIdx - tapsPerΦ + k ]
for k in 1:tapsPer𝜙
@inbounds accumulator += pfb[ k, 𝜙 ] * x[ inputIdx - tapsPer𝜙 + k ]
end

@inbounds buffer[yIdx] = accumulator
(Φ, inputIdx) = Φ == ? ( 1, inputIdx+1 ) : ( Φ+1, inputIdx )
(𝜙, inputIdx) = 𝜙 == N𝜙 ? ( 1, inputIdx+1 ) : ( 𝜙+1, inputIdx )
end

if xLen >= self.reqDlyLineLen
Expand Down Expand Up @@ -403,15 +403,15 @@ function filt!{T}( buffer::Vector{T}, self::FIRFilter{FIRRational}, x::Vector{T}
return T[]
end

outLen = outputlength( xLen-kernel.inputDeficit+1, kernel.ratio, kernel.ΦIdx )
outLen = outputlength( xLen-kernel.inputDeficit+1, kernel.ratio, kernel.𝜙Idx )
bufLen >= outLen || error( "buffer is too small" )

pfb::PFB{T} = kernel.pfb
dlyLine::Vector{T} = self.dlyLine
interpolation = num( kernel.ratio )
decimation = den( kernel.ratio )
ΦIdxStepSize = mod( decimation, interpolation )
criticalΦIdx = kernel. - ΦIdxStepSize
𝜙IdxStepSize = mod( decimation, interpolation )
critical𝜙Idx = kernel.N𝜙 - 𝜙IdxStepSize

inputIdx = kernel.inputDeficit
yIdx = 0
Expand All @@ -421,29 +421,29 @@ function filt!{T}( buffer::Vector{T}, self::FIRFilter{FIRRational}, x::Vector{T}
accumulator = zero( T )
yIdx += 1

if inputIdx < kernel.tapsPerΦ
if inputIdx < kernel.tapsPer𝜙
hIdx = 1
for k in inputIdx:self.reqDlyLineLen
@inbounds accumulator += pfb[ hIdx, kernel.ΦIdx ] * dlyLine[ k ]
@inbounds accumulator += pfb[ hIdx, kernel.𝜙Idx ] * dlyLine[ k ]
hIdx += 1
end

for k in 1:inputIdx
@inbounds accumulator += pfb[ hIdx, kernel.ΦIdx ] * x[ k ]
@inbounds accumulator += pfb[ hIdx, kernel.𝜙Idx ] * x[ k ]
hIdx += 1
end
else
hIdx = 1
for k in inputIdx-kernel.tapsPerΦ+1:inputIdx
@inbounds accumulator += pfb[ hIdx, kernel.ΦIdx ] * x[ k ]
for k in inputIdx-kernel.tapsPer𝜙+1:inputIdx
@inbounds accumulator += pfb[ hIdx, kernel.𝜙Idx ] * x[ k ]
hIdx += 1
end
end

buffer[ yIdx ] = accumulator

inputIdx += ifloor( ( kernel.ΦIdx + decimation - 1 ) / interpolation )
kernel.ΦIdx = nextphase( kernel.ΦIdx, kernel.ratio )
inputIdx += ifloor( ( kernel.𝜙Idx + decimation - 1 ) / interpolation )
kernel.𝜙Idx = nextphase( kernel.𝜙Idx, kernel.ratio )
end

kernel.inputDeficit = inputIdx - xLen
Expand Down

0 comments on commit 3b284aa

Please sign in to comment.