HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Optimizing code from spectral subtraction algorithm

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
subtractionalgorithmoptimizingspectralcodefrom

Problem

This is part of the code from a spectral subtraction algorithm. I'm trying to optimize it for Android.

Matlab code:

function Seg=segment(signal,W,SP,Window)

% SEGMENT chops a signal to overlapping windowed segments
% A= SEGMENT(X,W,SP,WIN) returns a matrix which its columns are segmented
% and windowed frames of the input one dimentional signal, X. W is the
% number of samples per window, default value W=256. SP is the shift
% percentage, default value SP=0.4. WIN is the window that is multiplied by
% each segment and its length should be W. the default window is hamming
% window.
% 06-Sep-04
% Esfandiar Zavarehei

if nargin<3
    SP=.4;
end
if nargin<2
    W=256;
end
if nargin<4
    Window=hamming(W);
end
Window=Window(:); %make it a column vector

L=length(signal);
SP=fix(W.*SP);
N=fix((L-W)/SP +1); %number of segments

Index=(repmat(1:W,N,1)+repmat((0:(N-1))'*SP,1,W))';
hw=repmat(Window,1,N);
Seg=signal(Index).*hw;


Java code:

```
public class MatrixAndSegments
{
public int numberOfSegments;
public double[][] res;

public MatrixAndSegments(int numberOfSegments,double[][] res)
{
this.numberOfSegments = numberOfSegments;
this.res = res;
}
}

public MatrixAndSegments segment (double[] signal_in,int samplesPerWindow, double shiftPercentage, double[] window)
{
//default shiftPercentage = 0.4
//default samplesPerWindow = 256 //W
//default window = hanning

int L = signal_in.length;
shiftPercentage = fix(samplesPerWindow * shiftPercentage); //SP
int numberOfSegments = fix ( (L - samplesPerWindow)/ shiftPercentage + 1); //N

double[][] reprowMatrix = reprowtrans(samplesPerWindow,numberOfSegments);
double[][] repcolMatrix = repcoltrans(numberOfSegments, shiftPercentage,samplesPerWindow );

//Index=(repmat(1:W,N,1)+repmat((0:(N-1))'*SP,1,W))';
double[][] index = new double[samplesPerWindow+1][numberOfSegments+1];

for (int x = 1; x < samplesPerWindow+1; x++ )
{

Solution

I don't have a clue about spectral subtraction, so just some minor, general notes about code:

-

public int fix(double val) {
    if (val < 0) {
        return (int) Math.ceil(val);
    }
    return (int) Math.floor(val);
}


It should be called truncate.

-
A few variables could be renamed to follow the Java code conventions.

  • Variables, parameters:



  • signal_in to inputSignal or signal



  • HW to hw



  • seg maybe to segments or segment (?)



  • Matrixseg to matrixSeg



  • Methods:



  • HammingWindow to calculateHammingWindow



(Effective Java, 2nd edition, Item 56: Adhere to generally accepted naming conventions and The Java Language Specification, Java SE 7 Edition, 6.1 Declarations).

-
This comments looks unnecessary:

// hamming window
double[] hammingWindow = this.HammingWindow(samplesPerWindow);


(Clean Code by Robert C. Martin: Chapter 4: Comments, Noise Comments)

The this. prefix also unnecessary.

Code Snippets

public int fix(double val) {
    if (val < 0) {
        return (int) Math.ceil(val);
    }
    return (int) Math.floor(val);
}
// hamming window
double[] hammingWindow = this.HammingWindow(samplesPerWindow);

Context

StackExchange Code Review Q#14815, answer score: 5

Revisions (0)

No revisions yet.