Giter Club home page Giter Club logo

sincmaths's Introduction

SincMaths

DOI

SincMaths is a Kotlin Multiplatform library which provides a 2D matrix SincMatrix to facilitate translation of signal processing code written in Octave/MATLAB to mobile applications. Originally written for the Gait&Balance app. See docs for a comprehensive list of functions. Key implementation aspects:

  • Stores data in row-major format as opposed to column-major format used by Octave/MATLAB
  • Uses ejml-simple for Android side optimisations
  • Uses Apple Accelerate for iOS side optimisations
  • Uses rafat/wavelib to provide wavelet-based demonising
  • Uses codeplea/tinyexpr to provide Octave/MATLAB style indexing

Examples

Initialisation

val matrixA = matrixFrom("[1, 2, 3; 4, 5, 6]")
val matrixB = matrixFrom("[1, 2, 3, 4]")
val matrixC = matrixFrom("[5;6;7;8;9;10]")
val matrixD = matrixFrom("1:10")
val matrixE = matrixFrom("-1.5:-1:-7.9")

val matrixF = matrixOf(2, 10, 1..20)
val matrixG = colVectorOf(1.0, 0.5, 2.9, 10.1, 15.4)

Indexing

val matrixM: SincMatrix = (1..110).asSincMatrix(11, 10)

val matrixA = matrixM["1:5,4:7"]
val matrixB = matrixM["1:end,end:-1:end-1"]

val matrixC = matrixM[3, 3]
val matrixD = matrixM[1]

val matrixE = matrixM[1..4]

val matrixF = matrixM.get { endR, endC, allR, allC ->
    Pair(allR, 4..7)
} // same as matrixM[":,4:7"]

Implementation of acf from MATLAB Central

Reference: https://au.mathworks.com/matlabcentral/fileexchange/30540-autocorrelation-function-acf

fun SincMatrix.acf(numLags: Int): SincMatrix {

    require(this.isVector) { "This function works only for vectors" }
    require(numLags < this.numel) {
        "No. of lags should be smaller than the length of the vector"
    }

    val zeroMeanVector = this - this.mean().scalar
    val convSum = zeroMeanVector.conv(bVector = zeroMeanVector.flip())
    val scale = 1.0 / zeroMeanVector.dot(zeroMeanVector).scalar
    val scaledConvSum = convSum * scale
    val acfElements = this.numel + 1..this.numel + numLags
    return if (this.isRow) {
        scaledConvSum[this.rowIndicesRange, acfElements]
    } else {
        scaledConvSum[acfElements, this.colIndicesRange]
    }
}

Usage

Kotlin Project: MavenCentral

Note: This library does not work with iosSimulatorArm64 target. Android tests have to be performed on an emulator or a connected device, otherwise, the underlying JAVA JNI libraries fail to load.

Add dependency:

implementation("io.github.gallvp:sincmathsmp:0.3")

Try a test:

expect(5050.0) {
    matrixFrom("1:100").sum().scalar
}

Swift Project: CocoaPods

Note: Global functions such as matrixFrom are converted into open class functions as SincMatrixInitKt.matrixFrom. Use 'Jump to Definition' on 'SincMaths' to locate the class and function definitions.

Clone this project and add it to your project's Podfile:

pod 'SincMaths', :path => '/path/to/cloned/sincmaths/sincmathsmp'

Import it in your project and try a test:

import SincMaths

XCTAssertEqual(
    SincMatrixInitKt.matrixFrom(script: "1:100").sum(dim: 1).scalar,
    5050.0
)

sincmaths's People

Contributors

gallvp avatar

Watchers

 avatar

sincmaths's Issues

Space in MATLAB type scripting

This does not work:

accelMLxAPxVert["(2*$fs)+1:end, :"], gyroData["(2*$fs)+1:end, :"]

where as this works:

accelMLxAPxVert["(2*$fs)+1:end,:"], gyroData["(2*$fs)+1:end,:"]

Difference is the spacing in , :

`all` function not working as expected

In MATLAB,

a=[2.842170943040401E-14, NaN, 0.0, NaN, 4.440892098500626E-15, NaN, 3.3306690738754696E-15, NaN];
all(a < 1e-10)

ans =

  logical

   0

In sincmaths,

val a = rowVectorOf(2.842170943040401E-14, NaN, 0.0, NaN, 4.440892098500626E-15, NaN, 3.3306690738754696E-15, NaN)

(a lt 1E-10).all()

true

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.