Advertisement
Guest User

resampler

a guest
Mar 29th, 2014
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 23.67 KB | None | 0 0
  1. /**
  2.  * A highly experimental resampler with hardcoded calculations
  3.  */
  4. object Resampler
  5. {
  6.    
  7.     //#########################################################
  8.     //###  DECIMATION : 2
  9.     //#########################################################
  10.     private val c0200 = -0.00000
  11.     private val c0201 = 6.73393e-18
  12.     private val c0202 = 0.287914
  13.     private val c0203 = 0.452254
  14.     private val c0204 = 0.109973
  15.     private val c0205 = 0.00000
  16.  
  17.     //#########################################################
  18.     //###  DECIMATION : 3
  19.     //#########################################################
  20.     private val c0300 = -0.00000
  21.     private val c0301 = -0.00665934
  22.     private val c0302 = 0.0318310
  23.     private val c0303 = 0.181130
  24.     private val c0304 = 0.318310
  25.     private val c0305 = 0.271694
  26.     private val c0306 = 0.106103
  27.     private val c0307 = 0.00932308
  28.     private val c0308 = -0.00000
  29.  
  30.     //#########################################################
  31.     //###  DECIMATION : 4
  32.     //#########################################################
  33.     private val c0400 = -0.00000
  34.     private val c0401 = -0.00357305
  35.     private val c0402 = 2.84852e-18
  36.     private val c0403 = 0.0428519
  37.     private val c0404 = 0.131690
  38.     private val c0405 = 0.220520
  39.     private val c0406 = 0.244937
  40.     private val c0407 = 0.186237
  41.     private val c0408 = 0.0909025
  42.     private val c0409 = 0.0219296
  43.     private val c0410 = 7.73526e-19
  44.     private val c0411 = -0.00000
  45.  
  46.     //#########################################################
  47.     //###  DECIMATION : 5
  48.     //#########################################################
  49.     private val c0500 = -0.00000
  50.     private val c0501 = -0.00196172
  51.     private val c0502 = -0.00336679
  52.     private val c0503 = 0.00849726
  53.     private val c0504 = 0.0449745
  54.     private val c0505 = 0.103355
  55.     private val c0506 = 0.163178
  56.     private val c0507 = 0.196726
  57.     private val c0508 = 0.186985
  58.     private val c0509 = 0.139359
  59.     private val c0510 = 0.0778281
  60.     private val c0511 = 0.0286021
  61.     private val c0512 = 0.00411497
  62.     private val c0513 = -0.000885547
  63.     private val c0514 = -0.00000
  64.  
  65.     //#########################################################
  66.     //###  DECIMATION : 6
  67.     //#########################################################
  68.     private val c0600 = -0.00000
  69.     private val c0601 = -0.00116344
  70.     private val c0602 = -0.00296700
  71.     private val c0603 = 1.80051e-18
  72.     private val c0604 = 0.0144470
  73.     private val c0605 = 0.0438880
  74.     private val c0606 = 0.0850224
  75.     private val c0607 = 0.127510
  76.     private val c0608 = 0.157800
  77.     private val c0609 = 0.165248
  78.     private val c0610 = 0.147236
  79.     private val c0611 = 0.110447
  80.     private val c0612 = 0.0675699
  81.     private val c0613 = 0.0312787
  82.     private val c0614 = 0.00882135
  83.     private val c0615 = 8.47823e-19
  84.     private val c0616 = -0.000767670
  85.     private val c0617 = -0.00000
  86.  
  87.     //#########################################################
  88.     //###  DECIMATION : 7
  89.     //#########################################################
  90.     private val c0700 = -0.00000
  91.     private val c0701 = -0.000738756
  92.     private val c0702 = -0.00222959
  93.     private val c0703 = -0.00194649
  94.     private val c0704 = 0.00376483
  95.     private val c0705 = 0.0180421
  96.     private val c0706 = 0.0417122
  97.     private val c0707 = 0.0722011
  98.     private val c0708 = 0.103761
  99.     private val c0709 = 0.129071
  100.     private val c0710 = 0.141661
  101.     private val c0711 = 0.138195
  102.     private val c0712 = 0.119674
  103.     private val c0713 = 0.0910713
  104.     private val c0714 = 0.0595247
  105.     private val c0715 = 0.0318653
  106.     private val c0716 = 0.0124668
  107.     private val c0717 = 0.00224596
  108.     private val c0718 = -0.000901830
  109.     private val c0719 = -0.000571381
  110.     private val c0720 = -0.00000
  111.  
  112.     abstract class Resampler(val decimation: Int)
  113.     {
  114.         var d0 = 0.0
  115.         var d1 = 0.0
  116.         var d2 = 0.0
  117.         var d3 = 0.0
  118.         var d4 = 0.0
  119.         var d5 = 0.0
  120.         var d6 = 0.0
  121.         var d7 = 0.0
  122.         var d8 = 0.0
  123.         var d9 = 0.0
  124.    
  125.         var idx = 0
  126.         val buf = Array.fill(decimation)(0.0)
  127.    
  128.         def decimate(v: Double)(f: (Double) => Unit) : Unit
  129.        
  130.         def interpolate(v: Double)(f: (Double) => Unit) : Unit
  131.     }
  132.  
  133.  
  134.     class Resampler1 extends Resampler(1)
  135.     {
  136.         def decimate(v: Double)(f: Double => Unit) = f(v)
  137.         def interpolate(v: Double)(f: Double => Unit) = f(v)
  138.     }//Resampler1
  139.        
  140.     class Resampler2 extends Resampler(2)
  141.     {
  142.         def decimate(v: Double)(f: Double => Unit) =
  143.         {
  144.             buf(idx) = v
  145.             idx += 1
  146.             if (idx >= decimation)
  147.                 {
  148.                 idx = 0
  149.                 d0 = d2
  150.                 d1 = d3
  151.                 d2 = buf(0)
  152.                 d3 = buf(1)
  153.                 val sum = d0 * c0200 + d1 * c0202 + d2 * c0204 +
  154.                           d1 * c0201 + d2 * c0203 + d3 * c0205
  155.                 f(sum)
  156.                 }
  157.         }
  158.  
  159.         def interpolate(v: Double)(f: Double => Unit) =
  160.         {
  161.             d0 = d1 ; d1 = d2 ; d2 = v
  162.             f(d0 * c0200 + d1 * c0202 + d2 * c0204)
  163.             f(d0 * c0201 + d1 * c0203 + d2 * c0205)
  164.         }
  165.     }//Resampler2
  166.        
  167.     class Resampler3 extends Resampler(3)
  168.     {
  169.         def decimate(v: Double)(f: Double => Unit) =
  170.         {
  171.             buf(idx) = v
  172.             idx += 1
  173.             if (idx >= decimation)
  174.                 {
  175.                 idx = 0
  176.                 d0 = d3
  177.                 d1 = d4
  178.                 d2 = buf(0)
  179.                 d3 = buf(1)
  180.                 d4 = buf(2)
  181.                 val sum = d0 * c0300 + d1 * c0303 + d2 * c0306 +
  182.                           d1 * c0301 + d2 * c0304 + d3 * c0307 +
  183.                           d2 * c0302 + d3 * c0305 + d4 * c0308
  184.                 f(sum)
  185.                 }
  186.         }
  187.  
  188.         def interpolate(v: Double)(f: Double => Unit) =
  189.         {
  190.             d0 = d1 ; d1 = d2 ; d2 = v
  191.             f(d0 * c0300 + d1 * c0303 + d2 * c0306)
  192.             f(d0 * c0301 + d1 * c0304 + d2 * c0307)
  193.             f(d0 * c0302 + d1 * c0305 + d2 * c0308)
  194.         }
  195.     }//Resampler3
  196.        
  197.     class Resampler4 extends Resampler(4)
  198.     {
  199.         def decimate(v: Double)(f: Double => Unit) =
  200.         {
  201.             buf(idx) = v
  202.             idx += 1
  203.             if (idx >= decimation)
  204.                 {
  205.                 idx = 0
  206.                 d0 = d4
  207.                 d1 = d5
  208.                 d2 = buf(0)
  209.                 d3 = buf(1)
  210.                 d4 = buf(2)
  211.                 d5 = buf(3)
  212.                 val sum = d0 * c0400 + d1 * c0404 + d2 * c0408 +
  213.                           d1 * c0401 + d2 * c0405 + d3 * c0409 +
  214.                           d2 * c0402 + d3 * c0406 + d4 * c0410 +
  215.                           d3 * c0403 + d4 * c0407 + d5 * c0411
  216.                 f(sum)
  217.                 }
  218.         }
  219.  
  220.         def interpolate(v: Double)(f: Double => Unit) =
  221.         {
  222.             d0 = d1 ; d1 = d2 ; d2 = v
  223.             f(d0 * c0400 + d1 * c0404 + d2 * c0408)
  224.             f(d0 * c0401 + d1 * c0405 + d2 * c0409)
  225.             f(d0 * c0402 + d1 * c0406 + d2 * c0410)
  226.             f(d0 * c0403 + d1 * c0407 + d2 * c0411)
  227.         }
  228.     }//Resampler4
  229.        
  230.     class Resampler5 extends Resampler(5)
  231.     {
  232.         def decimate(v: Double)(f: Double => Unit) =
  233.         {
  234.             buf(idx) = v
  235.             idx += 1
  236.             if (idx >= decimation)
  237.                 {
  238.                 idx = 0
  239.                 d0 = d5
  240.                 d1 = d6
  241.                 d2 = buf(0)
  242.                 d3 = buf(1)
  243.                 d4 = buf(2)
  244.                 d5 = buf(3)
  245.                 d6 = buf(4)
  246.                 val sum = d0 * c0500 + d1 * c0505 + d2 * c0510 +
  247.                           d1 * c0501 + d2 * c0506 + d3 * c0511 +
  248.                           d2 * c0502 + d3 * c0507 + d4 * c0512 +
  249.                           d3 * c0503 + d4 * c0508 + d5 * c0513 +
  250.                           d4 * c0504 + d5 * c0509 + d6 * c0514
  251.                 f(sum)
  252.                 }
  253.         }
  254.  
  255.         def interpolate(v: Double)(f: Double => Unit) =
  256.         {
  257.             d0 = d1 ; d1 = d2 ; d2 = v
  258.             f(d0 * c0500 + d1 * c0505 + d2 * c0510)
  259.             f(d0 * c0501 + d1 * c0506 + d2 * c0511)
  260.             f(d0 * c0502 + d1 * c0507 + d2 * c0512)
  261.             f(d0 * c0503 + d1 * c0508 + d2 * c0513)
  262.             f(d0 * c0504 + d1 * c0509 + d2 * c0514)
  263.         }
  264.     }//Resampler5
  265.        
  266.     class Resampler6 extends Resampler(6)
  267.     {
  268.         def decimate(v: Double)(f: Double => Unit) =
  269.         {
  270.             buf(idx) = v
  271.             idx += 1
  272.             if (idx >= decimation)
  273.                 {
  274.                 idx = 0
  275.                 d0 = d6
  276.                 d1 = d7
  277.                 d2 = buf(0)
  278.                 d3 = buf(1)
  279.                 d4 = buf(2)
  280.                 d5 = buf(3)
  281.                 d6 = buf(4)
  282.                 d7 = buf(5)
  283.                 val sum = d0 * c0600 + d1 * c0606 + d2 * c0612 +
  284.                           d1 * c0601 + d2 * c0607 + d3 * c0613 +
  285.                           d2 * c0602 + d3 * c0608 + d4 * c0614 +
  286.                           d3 * c0603 + d4 * c0609 + d5 * c0615 +
  287.                           d4 * c0604 + d5 * c0610 + d6 * c0616 +
  288.                           d5 * c0605 + d6 * c0611 + d7 * c0617
  289.                 f(sum)
  290.                 }
  291.         }
  292.  
  293.         def interpolate(v: Double)(f: Double => Unit) =
  294.         {
  295.             d0 = d1 ; d1 = d2 ; d2 = v
  296.             f(d0 * c0600 + d1 * c0606 + d2 * c0612)
  297.             f(d0 * c0601 + d1 * c0607 + d2 * c0613)
  298.             f(d0 * c0602 + d1 * c0608 + d2 * c0614)
  299.             f(d0 * c0603 + d1 * c0609 + d2 * c0615)
  300.             f(d0 * c0604 + d1 * c0610 + d2 * c0616)
  301.             f(d0 * c0605 + d1 * c0611 + d2 * c0617)
  302.         }
  303.     }//Resampler6
  304.        
  305.     class Resampler7 extends Resampler(7)
  306.     {
  307.         def decimate(v: Double)(f: Double => Unit) =
  308.         {
  309.             buf(idx) = v
  310.             idx += 1
  311.             if (idx >= decimation)
  312.                 {
  313.                 idx = 0
  314.                 d0 = d7
  315.                 d1 = d8
  316.                 d2 = buf(0)
  317.                 d3 = buf(1)
  318.                 d4 = buf(2)
  319.                 d5 = buf(3)
  320.                 d6 = buf(4)
  321.                 d7 = buf(5)
  322.                 d8 = buf(6)
  323.                 val sum = d0 * c0700 + d1 * c0707 + d2 * c0714 +
  324.                           d1 * c0701 + d2 * c0708 + d3 * c0715 +
  325.                           d2 * c0702 + d3 * c0709 + d4 * c0716 +
  326.                           d3 * c0703 + d4 * c0710 + d5 * c0717 +
  327.                           d4 * c0704 + d5 * c0711 + d6 * c0718 +
  328.                           d5 * c0705 + d6 * c0712 + d7 * c0719 +
  329.                           d6 * c0706 + d7 * c0713 + d8 * c0720
  330.                 f(sum)
  331.                 }
  332.         }
  333.  
  334.         def interpolate(v: Double)(f: Double => Unit) =
  335.         {
  336.             d0 = d1 ; d1 = d2 ; d2 = v
  337.             f(d0 * c0700 + d1 * c0707 + d2 * c0714)
  338.             f(d0 * c0701 + d1 * c0708 + d2 * c0715)
  339.             f(d0 * c0702 + d1 * c0709 + d2 * c0716)
  340.             f(d0 * c0703 + d1 * c0710 + d2 * c0717)
  341.             f(d0 * c0704 + d1 * c0711 + d2 * c0718)
  342.             f(d0 * c0705 + d1 * c0712 + d2 * c0719)
  343.             f(d0 * c0706 + d1 * c0713 + d2 * c0720)
  344.         }
  345.     }//Resampler7
  346.    
  347.    
  348.     def apply(decimation: Int) : Resampler =
  349.         {
  350.         decimation match
  351.             {
  352.             case 2 => new Resampler2
  353.             case 3 => new Resampler3
  354.             case 4 => new Resampler4
  355.             case 5 => new Resampler5
  356.             case 6 => new Resampler6
  357.             case 7 => new Resampler7
  358.             case _ => throw new IllegalArgumentException("Decimation " + decimation + " not supported")
  359.             }
  360.         }
  361. }
  362.  
  363.  
  364. /**
  365.  * A highly experimental resampler with hardcoded calculations
  366.  */
  367. object ResamplerX
  368. {
  369.    
  370.     //#########################################################
  371.     //###  DECIMATION : 2
  372.     //#########################################################
  373.     private val c0200 = -0.00000
  374.     private val c0201 = 6.73393e-18
  375.     private val c0202 = 0.287914
  376.     private val c0203 = 0.452254
  377.     private val c0204 = 0.109973
  378.     private val c0205 = 0.00000
  379.  
  380.     //#########################################################
  381.     //###  DECIMATION : 3
  382.     //#########################################################
  383.     private val c0300 = -0.00000
  384.     private val c0301 = -0.00665934
  385.     private val c0302 = 0.0318310
  386.     private val c0303 = 0.181130
  387.     private val c0304 = 0.318310
  388.     private val c0305 = 0.271694
  389.     private val c0306 = 0.106103
  390.     private val c0307 = 0.00932308
  391.     private val c0308 = -0.00000
  392.  
  393.     //#########################################################
  394.     //###  DECIMATION : 4
  395.     //#########################################################
  396.     private val c0400 = -0.00000
  397.     private val c0401 = -0.00357305
  398.     private val c0402 = 2.84852e-18
  399.     private val c0403 = 0.0428519
  400.     private val c0404 = 0.131690
  401.     private val c0405 = 0.220520
  402.     private val c0406 = 0.244937
  403.     private val c0407 = 0.186237
  404.     private val c0408 = 0.0909025
  405.     private val c0409 = 0.0219296
  406.     private val c0410 = 7.73526e-19
  407.     private val c0411 = -0.00000
  408.  
  409.     //#########################################################
  410.     //###  DECIMATION : 5
  411.     //#########################################################
  412.     private val c0500 = -0.00000
  413.     private val c0501 = -0.00196172
  414.     private val c0502 = -0.00336679
  415.     private val c0503 = 0.00849726
  416.     private val c0504 = 0.0449745
  417.     private val c0505 = 0.103355
  418.     private val c0506 = 0.163178
  419.     private val c0507 = 0.196726
  420.     private val c0508 = 0.186985
  421.     private val c0509 = 0.139359
  422.     private val c0510 = 0.0778281
  423.     private val c0511 = 0.0286021
  424.     private val c0512 = 0.00411497
  425.     private val c0513 = -0.000885547
  426.     private val c0514 = -0.00000
  427.  
  428.     //#########################################################
  429.     //###  DECIMATION : 6
  430.     //#########################################################
  431.     private val c0600 = -0.00000
  432.     private val c0601 = -0.00116344
  433.     private val c0602 = -0.00296700
  434.     private val c0603 = 1.80051e-18
  435.     private val c0604 = 0.0144470
  436.     private val c0605 = 0.0438880
  437.     private val c0606 = 0.0850224
  438.     private val c0607 = 0.127510
  439.     private val c0608 = 0.157800
  440.     private val c0609 = 0.165248
  441.     private val c0610 = 0.147236
  442.     private val c0611 = 0.110447
  443.     private val c0612 = 0.0675699
  444.     private val c0613 = 0.0312787
  445.     private val c0614 = 0.00882135
  446.     private val c0615 = 8.47823e-19
  447.     private val c0616 = -0.000767670
  448.     private val c0617 = -0.00000
  449.  
  450.     //#########################################################
  451.     //###  DECIMATION : 7
  452.     //#########################################################
  453.     private val c0700 = -0.00000
  454.     private val c0701 = -0.000738756
  455.     private val c0702 = -0.00222959
  456.     private val c0703 = -0.00194649
  457.     private val c0704 = 0.00376483
  458.     private val c0705 = 0.0180421
  459.     private val c0706 = 0.0417122
  460.     private val c0707 = 0.0722011
  461.     private val c0708 = 0.103761
  462.     private val c0709 = 0.129071
  463.     private val c0710 = 0.141661
  464.     private val c0711 = 0.138195
  465.     private val c0712 = 0.119674
  466.     private val c0713 = 0.0910713
  467.     private val c0714 = 0.0595247
  468.     private val c0715 = 0.0318653
  469.     private val c0716 = 0.0124668
  470.     private val c0717 = 0.00224596
  471.     private val c0718 = -0.000901830
  472.     private val c0719 = -0.000571381
  473.     private val c0720 = -0.00000
  474.  
  475.     abstract class Resampler(val decimation: Int)
  476.     {
  477.         var d0 = Complex(0.0)
  478.         var d1 = Complex(0.0)
  479.         var d2 = Complex(0.0)
  480.         var d3 = Complex(0.0)
  481.         var d4 = Complex(0.0)
  482.         var d5 = Complex(0.0)
  483.         var d6 = Complex(0.0)
  484.         var d7 = Complex(0.0)
  485.         var d8 = Complex(0.0)
  486.         var d9 = Complex(0.0)
  487.    
  488.         var idx = 0
  489.         val buf = Array.fill(decimation)(Complex(0.0))
  490.    
  491.         def decimate(v: Complex)(f: (Complex) => Unit) : Unit
  492.        
  493.         def interpolate(v: Complex)(f: (Complex) => Unit) : Unit
  494.     }
  495.  
  496.     class Resampler1 extends Resampler(1)
  497.     {
  498.         def decimate(v: Complex)(f: Complex => Unit) = f(v)
  499.         def interpolate(v: Complex)(f: Complex => Unit) = f(v)
  500.     }//Resampler1
  501.        
  502.      class Resampler2 extends Resampler(2)
  503.     {
  504.         def decimate(v: Complex)(f: Complex => Unit) =
  505.         {
  506.             buf(idx) = v
  507.             idx += 1
  508.             if (idx >= decimation)
  509.                 {
  510.                 idx = 0
  511.                 d0 = d2
  512.                 d1 = d3
  513.                 d2 = buf(0)
  514.                 d3 = buf(1)
  515.                 val sum = d0 * c0200 + d1 * c0202 + d2 * c0204 +
  516.                           d1 * c0201 + d2 * c0203 + d3 * c0205
  517.                 f(sum)
  518.                 }
  519.         }
  520.  
  521.         def interpolate(v: Complex)(f: Complex => Unit) =
  522.         {
  523.             d0 = d1 ; d1 = d2 ; d2 = v
  524.             f(d0 * c0200 + d1 * c0202 + d2 * c0204)
  525.             f(d0 * c0201 + d1 * c0203 + d2 * c0205)
  526.         }
  527.     }//Resampler2
  528.        
  529.     class Resampler3 extends Resampler(3)
  530.     {
  531.         def decimate(v: Complex)(f: Complex => Unit) =
  532.         {
  533.             buf(idx) = v
  534.             idx += 1
  535.             if (idx >= decimation)
  536.                 {
  537.                 idx = 0
  538.                 d0 = d3
  539.                 d1 = d4
  540.                 d2 = buf(0)
  541.                 d3 = buf(1)
  542.                 d4 = buf(2)
  543.                 val sum = d0 * c0300 + d1 * c0303 + d2 * c0306 +
  544.                           d1 * c0301 + d2 * c0304 + d3 * c0307 +
  545.                           d2 * c0302 + d3 * c0305 + d4 * c0308
  546.                 f(sum)
  547.                 }
  548.         }
  549.  
  550.         def interpolate(v: Complex)(f: Complex => Unit) =
  551.         {
  552.             d0 = d1 ; d1 = d2 ; d2 = v
  553.             f(d0 * c0300 + d1 * c0303 + d2 * c0306)
  554.             f(d0 * c0301 + d1 * c0304 + d2 * c0307)
  555.             f(d0 * c0302 + d1 * c0305 + d2 * c0308)
  556.         }
  557.     }//Resampler3
  558.        
  559.     class Resampler4 extends Resampler(4)
  560.     {
  561.         def decimate(v: Complex)(f: Complex => Unit) =
  562.         {
  563.             buf(idx) = v
  564.             idx += 1
  565.             if (idx >= decimation)
  566.                 {
  567.                 idx = 0
  568.                 d0 = d4
  569.                 d1 = d5
  570.                 d2 = buf(0)
  571.                 d3 = buf(1)
  572.                 d4 = buf(2)
  573.                 d5 = buf(3)
  574.                 val sum = d0 * c0400 + d1 * c0404 + d2 * c0408 +
  575.                           d1 * c0401 + d2 * c0405 + d3 * c0409 +
  576.                           d2 * c0402 + d3 * c0406 + d4 * c0410 +
  577.                           d3 * c0403 + d4 * c0407 + d5 * c0411
  578.                 f(sum)
  579.                 }
  580.         }
  581.  
  582.         def interpolate(v: Complex)(f: Complex => Unit) =
  583.         {
  584.             d0 = d1 ; d1 = d2 ; d2 = v
  585.             f(d0 * c0400 + d1 * c0404 + d2 * c0408)
  586.             f(d0 * c0401 + d1 * c0405 + d2 * c0409)
  587.             f(d0 * c0402 + d1 * c0406 + d2 * c0410)
  588.             f(d0 * c0403 + d1 * c0407 + d2 * c0411)
  589.         }
  590.     }//Resampler4
  591.        
  592.     class Resampler5 extends Resampler(5)
  593.     {
  594.         def decimate(v: Complex)(f: Complex => Unit) =
  595.         {
  596.             buf(idx) = v
  597.             idx += 1
  598.             if (idx >= decimation)
  599.                 {
  600.                 idx = 0
  601.                 d0 = d5
  602.                 d1 = d6
  603.                 d2 = buf(0)
  604.                 d3 = buf(1)
  605.                 d4 = buf(2)
  606.                 d5 = buf(3)
  607.                 d6 = buf(4)
  608.                 val sum = d0 * c0500 + d1 * c0505 + d2 * c0510 +
  609.                           d1 * c0501 + d2 * c0506 + d3 * c0511 +
  610.                           d2 * c0502 + d3 * c0507 + d4 * c0512 +
  611.                           d3 * c0503 + d4 * c0508 + d5 * c0513 +
  612.                           d4 * c0504 + d5 * c0509 + d6 * c0514
  613.                 f(sum)
  614.                 }
  615.         }
  616.  
  617.         def interpolate(v: Complex)(f: Complex => Unit) =
  618.         {
  619.             d0 = d1 ; d1 = d2 ; d2 = v
  620.             f(d0 * c0500 + d1 * c0505 + d2 * c0510)
  621.             f(d0 * c0501 + d1 * c0506 + d2 * c0511)
  622.             f(d0 * c0502 + d1 * c0507 + d2 * c0512)
  623.             f(d0 * c0503 + d1 * c0508 + d2 * c0513)
  624.             f(d0 * c0504 + d1 * c0509 + d2 * c0514)
  625.         }
  626.     }//Resampler5
  627.        
  628.     class Resampler6 extends Resampler(6)
  629.     {
  630.         def decimate(v: Complex)(f: Complex => Unit) =
  631.         {
  632.             buf(idx) = v
  633.             idx += 1
  634.             if (idx >= decimation)
  635.                 {
  636.                 idx = 0
  637.                 d0 = d6
  638.                 d1 = d7
  639.                 d2 = buf(0)
  640.                 d3 = buf(1)
  641.                 d4 = buf(2)
  642.                 d5 = buf(3)
  643.                 d6 = buf(4)
  644.                 d7 = buf(5)
  645.                 val sum = d0 * c0600 + d1 * c0606 + d2 * c0612 +
  646.                           d1 * c0601 + d2 * c0607 + d3 * c0613 +
  647.                           d2 * c0602 + d3 * c0608 + d4 * c0614 +
  648.                           d3 * c0603 + d4 * c0609 + d5 * c0615 +
  649.                           d4 * c0604 + d5 * c0610 + d6 * c0616 +
  650.                           d5 * c0605 + d6 * c0611 + d7 * c0617
  651.                 f(sum)
  652.                 }
  653.         }
  654.  
  655.         def interpolate(v: Complex)(f: Complex => Unit) =
  656.         {
  657.             d0 = d1 ; d1 = d2 ; d2 = v
  658.             f(d0 * c0600 + d1 * c0606 + d2 * c0612)
  659.             f(d0 * c0601 + d1 * c0607 + d2 * c0613)
  660.             f(d0 * c0602 + d1 * c0608 + d2 * c0614)
  661.             f(d0 * c0603 + d1 * c0609 + d2 * c0615)
  662.             f(d0 * c0604 + d1 * c0610 + d2 * c0616)
  663.             f(d0 * c0605 + d1 * c0611 + d2 * c0617)
  664.         }
  665.     }//Resampler6
  666.        
  667.     class Resampler7 extends Resampler(7)
  668.     {
  669.         def decimate(v: Complex)(f: Complex => Unit) =
  670.         {
  671.             buf(idx) = v
  672.             idx += 1
  673.             if (idx >= decimation)
  674.                 {
  675.                 idx = 0
  676.                 d0 = d7
  677.                 d1 = d8
  678.                 d2 = buf(0)
  679.                 d3 = buf(1)
  680.                 d4 = buf(2)
  681.                 d5 = buf(3)
  682.                 d6 = buf(4)
  683.                 d7 = buf(5)
  684.                 d8 = buf(6)
  685.                 val sum = d0 * c0700 + d1 * c0707 + d2 * c0714 +
  686.                           d1 * c0701 + d2 * c0708 + d3 * c0715 +
  687.                           d2 * c0702 + d3 * c0709 + d4 * c0716 +
  688.                           d3 * c0703 + d4 * c0710 + d5 * c0717 +
  689.                           d4 * c0704 + d5 * c0711 + d6 * c0718 +
  690.                           d5 * c0705 + d6 * c0712 + d7 * c0719 +
  691.                           d6 * c0706 + d7 * c0713 + d8 * c0720
  692.                 f(sum)
  693.                 }
  694.         }
  695.  
  696.         def interpolate(v: Complex)(f: Complex => Unit) =
  697.         {
  698.             d0 = d1 ; d1 = d2 ; d2 = v
  699.             f(d0 * c0700 + d1 * c0707 + d2 * c0714)
  700.             f(d0 * c0701 + d1 * c0708 + d2 * c0715)
  701.             f(d0 * c0702 + d1 * c0709 + d2 * c0716)
  702.             f(d0 * c0703 + d1 * c0710 + d2 * c0717)
  703.             f(d0 * c0704 + d1 * c0711 + d2 * c0718)
  704.             f(d0 * c0705 + d1 * c0712 + d2 * c0719)
  705.             f(d0 * c0706 + d1 * c0713 + d2 * c0720)
  706.         }
  707.     }//Resampler7
  708.    
  709.    
  710.     def apply(decimation: Int) : Resampler =
  711.         {
  712.         decimation match
  713.             {
  714.             case 1 => new Resampler1
  715.             case 2 => new Resampler2
  716.             case 3 => new Resampler3
  717.             case 4 => new Resampler4
  718.             case 5 => new Resampler5
  719.             case 6 => new Resampler6
  720.             case 7 => new Resampler7
  721.             case _ => throw new IllegalArgumentException("Decimation " + decimation + " not supported")
  722.             }
  723.         }
  724.  
  725.  
  726. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement