Random Vectors » NLP http://randomvectors.com I hope some people will find it to their advantage to decipher all this mess. Évariste Galois Mon, 26 Oct 2015 15:45:17 +0000 en-US hourly 1 Audio Analysis with WaveSurfer http://randomvectors.com/blog/2011/03/07/audio-analysis-with-wavesurfer/ http://randomvectors.com/blog/2011/03/07/audio-analysis-with-wavesurfer/#comments Mon, 07 Mar 2011 19:49:24 +0000 http://randomvectors.com/?p=216 Wave Surfer is under active development again and I am delighted.

http://sourceforge.net/projects/wavesurfer/

If you want a complete tool kit for analysis of audio this is it. The system runs on Windows, OSX and  Linux what more could you ask for? Precompiled binaries are available as well as source code.

Wave Surfer Sample Screen

Sample Screen

]]>
http://randomvectors.com/blog/2011/03/07/audio-analysis-with-wavesurfer/feed/ 0
Latent Semantic Indexing http://randomvectors.com/blog/2011/03/07/latent-semantic-indexing/ http://randomvectors.com/blog/2011/03/07/latent-semantic-indexing/#comments Mon, 07 Mar 2011 19:18:56 +0000 http://randomvectors.com/?p=205 Here is a toy program that demonstrates LSI using the SVD contained within the COLT linear algebra library. The method is taken from here.

import cern.colt.matrix.linalg.*;
import cern.colt.matrix.*;
import cern.colt.matrix.impl.*;
import java.util.*;
import java.io.*;

public class SVDText
{
 public static void main (String args[])
 {
    try
    {
       DenseDoubleMatrix2D source = new DenseDoubleMatrix2D(3,11);

       Scanner sc = new Scanner(new File("input.txt"));
       for (int row=0;row<11;row++)
       {
          for (int col=0;col<3;col++)
          {
             float value = sc.nextFloat();
             source.setQuick(col,row,value*1.0);
          }
       }

       DenseDoubleMatrix2D query = new DenseDoubleMatrix2D(1,11);

       sc = new Scanner(new File("query.txt"));
       for (int col=0;col<11;col++)
       {
          long value = sc.nextLong();
          query.setQuick(0,col,value*1.0);
       }

       Algebra alg = new Algebra();

       SingularValueDecomposition svd = new SingularValueDecomposition(source);

       // reduce rank
       DoubleMatrix2D reducedU = alg.subMatrix(alg.transpose(svd.getU()),0,1,0,10);
       DoubleMatrix2D reducedS = alg.subMatrix(alg.transpose(svd.getS()),0,1,0,1);
       DoubleMatrix2D reducedV = alg.subMatrix(alg.transpose(svd.getV()),0,1,0,2);

       DoubleMatrix2D reducedVt = alg.transpose(reducedV);

       DoubleMatrix2D inverseS = alg.pow(reducedS,-1);

       DoubleMatrix2D q1 = alg.mult(inverseS,reducedU);
       System.out.println("q1 = " + q1);
       DoubleMatrix1D queryVector = alg.mult(q1,alg.transpose(query)).viewRow(0);

       System.out.println("query vector " + queryVector);
       DoubleMatrix1D d1 = alg.subMatrix(reducedVt,0,0,0,1).viewColumn(0);
       System.out.println("d1 = " + d1);

       DoubleMatrix1D d2 = alg.subMatrix(reducedVt,1,1,0,1).viewColumn(0);
       System.out.println("d2 = " + d2);

       DoubleMatrix1D d3 = alg.subMatrix(reducedVt,2,2,0,1).viewColumn(0);

       System.out.println("Doc 1 measure = " + queryVector.zDotProduct(d1) / ((alg.norm1(queryVector)*alg.norm1(d1))));

       System.out.println("Doc 2 measure = " + queryVector.zDotProduct(d2) / ((alg.norm1(queryVector)*alg.norm1(d2))));

       System.out.println("Doc 3 measure = " + queryVector.zDotProduct(d3) / ((alg.norm1(queryVector)*alg.norm1(d3))));
   }
   catch (Exception e)
   {
      e.printStackTrace();
   }
 }
}
]]>
http://randomvectors.com/blog/2011/03/07/latent-semantic-indexing/feed/ 0
Curve Fitting via SVD http://randomvectors.com/blog/2011/03/07/curve-fitting-via-svd/ http://randomvectors.com/blog/2011/03/07/curve-fitting-via-svd/#comments Mon, 07 Mar 2011 19:02:43 +0000 http://randomvectors.com/?p=121 I used this method to delete a gradient effect from uneven lighting of some of my book scans.

Here is a simple method for curve fitting various polynomials. This works for non linear equations as long as the actual coeficients are linear.

For example you can NOT fit the following type of function:

Y={a_1cos(a_2x)+a_3x}

Note that the coeficients a_2 is an argument to a nonlinear function. related link

The specific one done here is a bicubic polynomial expressed by the following.

We are going to fit the following curve:

P(x,y) ={a_00+a_01y+a_02y^2+a_03y^3+a_10x+a_11xy+a_12xy^2+a_13xy^3+a_20x^2+a_21x^2y+a_22x^2y^2+a_23x^2y^3+a_30x^3+a_31x^3y+a_32x^3y^2+a_33x^3y^3}

Or more compactly:

P(x,y)=sum{i=0}{3}{sum{j=0}{3}{a_ij}{x^i}{y^j}}

 import cern.colt.matrix.linalg.*;
import cern.colt.matrix.*;
import cern.colt.matrix.impl.*;

import edu.umbc.cs.maple.utils.*;

 public DoubleMatrix2D calcFit(ArrayList<Point> measures)
 {
    int cols = 16; // number of coeficients
    int rows = measures.size();  // size of your dataset

    DenseDoubleMatrix2D M = new DenseDoubleMatrix2D(rows,cols);
    DenseDoubleMatrix2D P = new DenseDoubleMatrix2D(rows,1);

    for (int k=0;k<measures.size();k++)
    {
       Point measure = measures.get(k);  // measures from your dataset
       P.set(k,0,measure.getValue());    // Point contains values P , x and y

       double x = measure.getX();
       double y = measure.getY();

       // populate the matrix with the appropriate polynomial values
       // from the bicubic above
       int col=0;
       for (int i=0;i<4;i++)
         for (int j=0;j<4;j++)
         {
            double cal = pow(x,i)*pow(y,j);
            M.set(k,col++,cal);
         }
    }

    // solve SVD
    SingularValueDecomposition svd = new SingularValueDecomposition(M);
    DoubleMatrix2D U = svd.getU();
    DoubleMatrix2D V = svd.getV();
    DoubleMatrix2D S = svd.getS();

    // S is the diagonal matrix
    // Invert by replacing its diagonal elements by their reciprocals
    for (int i=0;i<16;i++)
    {
       double val = 1.0/S.get(i,i);

       if (S.get(i,i) == 0)  // avoid numeric underflow
         val = 0.0;

       S.set(i,i, val);
    }

    Algebra alg = new Algebra();

    // X pseudoinverse
    DoubleMatrix2D VS = alg.mult(V,S);

    DoubleMatrix2D Minv = alg.mult(VS,alg.transpose(U));

    // matrix has coeficients
    DoubleMatrix2D C = alg.mult(Minv, P);

   // If you would like the closeness of the fit use the following

   DoubleMatrix2D eP = alg.mult(M,C); // estimated values

   ColtUtils utils = new ColtUtils();
   DoubleMatrix2D dP = utils.minus(P,eP); // difference between estimated and observed values
  double sum = utils.dotproduct(utils.getcol(dP,0),utils.getcol(dP,0));

  System.out.println("sum = " + sum);  // how close was our fit?

  return(C);
}

In practice you may need to normalize your measurements if they are large to avoid numeric under and/or over flow.

]]>
http://randomvectors.com/blog/2011/03/07/curve-fitting-via-svd/feed/ 0