-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FFT-based convolutions w/ some great timing tests.
- Loading branch information
Showing
5 changed files
with
288 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package tech.v3.datatype; | ||
|
||
|
||
public final class Complex | ||
{ | ||
public static double mulReal( double ar, double ai, double br, double bi) { | ||
return ar*br - ai*bi; | ||
} | ||
public static double mulImg( double ar, double ai, double br, double bi) { | ||
return ar*bi + ai*br; | ||
} | ||
public static double[] mul(double[] lhs, int lhsOffset, double[] rhs, int rhsOffset, | ||
double[] result, int resOffset, int nElems) { | ||
final int lhsOff = lhsOffset * 2; | ||
final int rhsOff = rhsOffset * 2; | ||
final int resOff = resOffset * 2; | ||
for (int idx = 0; idx < nElems; ++idx ) { | ||
int localIdx = idx*2; | ||
result[localIdx+resOff] = mulReal(lhs[localIdx + lhsOff], | ||
lhs[localIdx + 1 + lhsOff], | ||
rhs[localIdx + rhsOff], | ||
rhs[localIdx + 1 + rhsOff]); | ||
result[localIdx+1+resOff] = mulImg(lhs[localIdx + lhsOff], | ||
lhs[localIdx + 1 + lhsOff], | ||
rhs[localIdx + rhsOff], | ||
rhs[localIdx + 1 + rhsOff]); | ||
} | ||
return result; | ||
} | ||
public static double[] mul(double[] lhs, double[] rhs) { | ||
return mul(lhs, 0, rhs, 0, new double[lhs.length], 0, | ||
lhs.length/2); | ||
} | ||
public static double[] realToComplex(double[] real, int off, double[] complex, | ||
int coff, int nElems) { | ||
final int coffset = coff * 2; | ||
for( int idx = 0; idx < nElems; ++idx ) { | ||
final int localCOff = coffset + idx*2; | ||
complex[localCOff] = real[idx+off]; | ||
complex[localCOff+1] = 0.0; | ||
} | ||
return complex; | ||
} | ||
public static double[] realToComplex(double[] real) { | ||
return realToComplex(real, 0, new double[real.length*2], 0, real.length); | ||
} | ||
public static double[] realToComplex(double real, double[] complex, int coff, | ||
int nElems) { | ||
final int coffset = coff * 2; | ||
for( int idx = 0; idx < nElems; ++idx ) { | ||
final int localCOff = coffset + idx*2; | ||
complex[localCOff] = real; | ||
complex[localCOff+1] = 0.0; | ||
} | ||
return complex; | ||
} | ||
public static double[] complexToReal(double[] complex, int coff, double[] real, | ||
int off, int nElems) { | ||
final int coffset = coff * 2; | ||
for(int idx = 0; idx < nElems; ++idx ) { | ||
real[idx+off] = complex[coffset + idx*2]; | ||
} | ||
return real; | ||
} | ||
public static double[] complexToReal(double[] complex) { | ||
return complexToReal(complex, 0, new double[complex.length/2], 0, complex.length/2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
(ns tech.v3.datatype.convolve-test | ||
(:require [tech.v3.datatype :as dtype] | ||
[tech.v3.datatype.convolve :as dt-conv] | ||
[tech.v3.datatype.functional :as dfn] | ||
[clojure.test :refer [deftest is]])) | ||
|
||
|
||
|
||
(deftest basetest | ||
(is (dfn/equals [0.000, 1.000, 2.500, 4.000, 1.500] | ||
(dt-conv/convolve1d [1, 2, 3], [0, 1, 0.5]))) | ||
(is (dfn/equals [0.000, 1.000, 2.500, 4.000, 1.500] | ||
(dt-conv/convolve1d [1, 2, 3], [0, 1, 0.5] | ||
{:algorithm :fft}))) | ||
(is (dfn/equals [1 2.5 4] | ||
(dt-conv/convolve1d [1, 2, 3], [0, 1, 0.5] {:mode :same}))) | ||
(is (dfn/equals [1 2.5 4] | ||
(dt-conv/convolve1d [1, 2, 3], [0, 1, 0.5] {:mode :same | ||
:algorithm :fft}))) | ||
|
||
(is (dfn/equals [2.5] | ||
(dt-conv/convolve1d [1, 2, 3], [0, 1, 0.5] {:mode :valid}))) | ||
(is (dfn/equals [2.5] | ||
(dt-conv/convolve1d [1, 2, 3], [0, 1, 0.5] {:mode :valid | ||
:algorithm :fft}))) | ||
|
||
(let [src-data (dfn/sin (range 0 20 0.1)) | ||
modes [:same :valid :full] | ||
edge-modes [:zero :clamp] | ||
window-sizes (range 5 15)] | ||
(->> (for [mode modes | ||
edge-mode edge-modes | ||
window-size window-sizes] | ||
(is (dfn/equals (dt-conv/convolve1d src-data (range window-size) | ||
{:mode mode :edge-mode edge-mode}) | ||
(dt-conv/convolve1d src-data (range window-size) | ||
{:mode mode :edge-mode edge-mode | ||
:algorithm :fft})) | ||
(format "Algorithm mismatch: mode %s edge-mode %s window-size %d" | ||
mode edge-mode window-size))) | ||
dorun))) |