# -*- tcl -*-
# Tests for filters library.
#
# RCS: @(#) $Id: geometry.test,v 1.13 2010/04/06 17:02:25 andreas_kupries Exp $

# -------------------------------------------------------------------------

source [file join \
        [file dirname [file dirname [file join [pwd] [info script]]]] \
        devtools testutilities.tcl]

testsNeedTcl     8.6
testsNeedTcltest 1.0

support {
    useLocal math.tcl math
}
testing {
    useLocal filtergen.tcl math::filters
}

# -------------------------------------------------------------------------

proc withFourDecimals {args} {
    set res {}
    foreach arg $args {lappend res [expr (round(10000*$arg))/10000.0]}
    return $res
}

if { [info commands lmap] eq {} } {
    proc lmap {var list body} {
        upvar 1 $var _$var
        set __$var {}
        foreach _$var $list {
            lappend __$var [uplevel 1 $body]
        }
        set __$var
    }
}

#
# Custom matching procedure:
# Expect an accuracy of at least four decimals
#
proc matchNumbers {expected actual} {
    set match 1
    foreach a $actual e $expected {
        if { [llength $a] == 1 } {
            if {abs($a-$e) > 1.0e-4} {
                set match 0
                break
            }
        } else {
            set match [matchNumbers $a $e]
        }
    }
    return $match
}

customMatch numbers matchNumbers


# -------------------------------------------------------------------------

# Butterworth filter coefficients

test butterworth-1.1 {low-pass, second order} -match numbers -body {
    set coeffs [::math::filters::filterButterworth 1 2 100 20]
    set result [concat {*}$coeffs]
} -result {2.0 1.0 0.20657 0.36953 -0.195816}

test butterworth-1.2 {high-pass, second order} -match numbers -body {
    set coeffs [::math::filters::filterButterworth 0 2 100 20]
    set result [concat {*}$coeffs]
} -result {-2.0 1.0 0.20657 0.36953 -0.195816}

test chebyshev-1.1 {low-pass, second order} -match numbers -body {
    set coeffs [::math::filters::filterChebyshev 1 2 100 20]
    set result [concat {*}$coeffs]
} -result {2.0 20.0 0.022604 -0.566099 -0.251249}

test chebyshev-1.2 {high-pass, second order} -match numbers -body {
    set coeffs [::math::filters::filterChebyshev 0 2 100 20]
    set result [concat {*}$coeffs]
} -result {-2.0 20.0 0.022604 -0.566099 -0.251249}


# Actually filter data

test filter-1.0 {low-pass, second order, uniform series, Butterworth} -match numbers -body {
    set coeffs   [::math::filters::filterButterworth 1 2 100 20]
    set data     [lrepeat 20 1.0]
    set filtered [::math::filters::filter $coeffs $data]
    set result   [list [llength $filtered] [lindex $filtered end]]
} -result {20 1.0}

test filter-1.1 {low-pass, second order, sine series, Butterworth} -match numbers -body {
    set coeffs   [::math::filters::filterButterworth 1 2 100 20]

    set twopi  [expr {2.0 * acos(-1.0)}]
    set period 100

    set data {}

    for {set i 0} {$i < $period} {incr i} {
        lappend data [expr {cos($twopi * ($i/1.0) / double($period))}]
    }

    set filtered [lrange [::math::filters::filter $coeffs $data] end-7 end]
} -result {0.845195 0.877086 0.905515 0.930371 0.951555 0.968984 0.982588 0.992315}

test filter-2.1 {low-pass, second order, sine series, Chebyshev} -match numbers -body {
    set coeffs   [::math::filters::filterChebyshev 1 2 100 20]

    set twopi  [expr {2.0 * acos(-1.0)}]
    set period 50

    set data {}

    for {set i 0} {$i < $period} {incr i} {
        lappend data [expr {cos($twopi * ($i/1.0) / double($period))}]
    }

    set filtered [lrange [::math::filters::filter $coeffs $data] end-7 end]
} -result {0.488962 0.593742 0.68916 0.773708 0.846055 0.905059 0.94979 0.979541}


test filter-2.2 {high-pass, second order, sine series, Chebyshev} -match numbers -body {
    set coeffs   [::math::filters::filterChebyshev 0 2 500 200]

    set twopi  [expr {2.0 * acos(-1.0)}]
    set period 50

    set data {}

    for {set i 0} {$i < $period} {incr i} {
        lappend data [expr {cos($twopi * ($i/1.0) / double($period))}]
    }

    set filtered [lrange [::math::filters::filter $coeffs $data] end-7 end]
} -result {-0.00144136 -0.0025429 -0.0031529 -0.00260315 -0.004107 -0.0029269 -0.00449589 -0.00327216}


# The object interface

test filterobj-1.0 {low-pass, second order, sine series} -match numbers -body {
    set coeffs   [::math::filters::filterButterworth 1 2 100 20]
    set filter   [::math::filters::filterObject new $coeffs]

    set twopi  [expr {2.0 * acos(-1.0)}]
    set period 100

    set data {}

    for {set i 0} {$i < $period} {incr i} {
        lappend filtered [$filter filter [expr {cos($twopi * ($i/1.0) / double($period))}]]
    }

    set filtered [lrange $filtered end-9 end]
} -result {0.771545 0.809968 0.845195 0.877086 0.905515 0.930371 0.951555 0.968984 0.982588 0.992315}


#
# Cleanup
#
testsuiteCleanup
