Skip to content

Commit

Permalink
Fixed missing javadocs and code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
psambit9791 committed Dec 20, 2023
1 parent 634520b commit 317e398
Show file tree
Hide file tree
Showing 30 changed files with 304 additions and 62 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<version>3.12.0</version>
<configuration>
<source>8</source>
<target>8</target>
<source>9</source>
<target>9</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/github/psambit9791/jdsp/filter/FIRWin2.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ public FIRWin2(int numTaps) {
}
}

/**
* Returns the filter type determined from numTaps
* @return int the filter type
*/
public int getFilterType() {
return this.ftype;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public interface _IIRFilter {
* @param cutoffFreq The cutoff frequency for the filter
* @return double[] Filtered signal
*/
public double[] lowPassFilter(double[] signal, int order, double cutoffFreq);
double[] lowPassFilter(double[] signal, int order, double cutoffFreq);

/**
* This method implements a high pass filter with given parameters, filters the signal and returns it.
Expand All @@ -37,7 +37,7 @@ public interface _IIRFilter {
* @param cutoffFreq The cutoff frequency for the filter
* @return double[] Filtered signal
*/
public double[] highPassFilter(double[] signal, int order, double cutoffFreq);
double[] highPassFilter(double[] signal, int order, double cutoffFreq);

/**
* This method implements a band pass filter with given parameters, filters the signal and returns it.
Expand All @@ -47,7 +47,7 @@ public interface _IIRFilter {
* @param highCutoff The upper cutoff frequency for the filter
* @return double[] Filtered signal
*/
public double[] bandPassFilter(double[] signal, int order, double lowCutoff, double highCutoff);
double[] bandPassFilter(double[] signal, int order, double lowCutoff, double highCutoff);

/**
* This method implements a band stop filter with given parameters, filters the signal and returns it.
Expand All @@ -57,6 +57,6 @@ public interface _IIRFilter {
* @param highCutoff The upper cutoff frequency for the filter
* @return double[] Filtered signal
*/
public double[] bandStopFilter(double[] signal, int order, double lowCutoff, double highCutoff);
double[] bandStopFilter(double[] signal, int order, double lowCutoff, double highCutoff);

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ public interface _KernelFilter {
* @param signal Signal to be filtered
* @return double[] Filtered signal
*/
public double[] filter(double[] signal);
double[] filter(double[] signal);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public interface _Adaptive {
* RANDOM: filter weights get an initial random value ranging from 0 to 1
* ZEROS: filter weights get initial value 0
*/
public enum WeightsFillMethod {
enum WeightsFillMethod {
RANDOM,
ZEROS
}
Expand All @@ -36,5 +36,5 @@ public enum WeightsFillMethod {
* @param desired desired signal that you want after filtering of x
* @param x input signal that you want to filter with the LMS adaptive filter to achieve the desired signal
*/
public void filter(double[] desired, double[] x);
void filter(double[] desired, double[] x);
}
6 changes: 6 additions & 0 deletions src/main/java/com/github/psambit9791/jdsp/io/WAV.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,12 @@ else if (type.equals("double")) {
}
}

/**
* Return data in provided datatype
* @param type Datatype provided. Must be one of int, long or double
* @return double[][] WAV data as a 2D array
* @throws java.lang.IllegalArgumentException if type is not one of int, long or double
*/
public double[][] getData(String type) throws IllegalArgumentException {
if (type.equals("int")) {
return this.dataInt;
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/github/psambit9791/jdsp/misc/Random.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public double randomNormalSample() {

/**
* Generate a 1D array of random samples from the normal distribution
* @param shape The dimension of the 1D array to generate. Must have only 1 value.
* @return double[] 1D array of random samples from the normal distribution
*/
public double[] randomNormal1D(int[] shape) {
Expand All @@ -94,6 +95,7 @@ public double[] randomNormal1D(int[] shape) {

/**
* Generate a 2D array of random samples from the normal distribution
* @param shape The dimension of the 2D array to generate. Must have only 2 values.
* @return double[][] 2D array of random samples from the normal distribution
*/
public double[][] randomNormal2D(int[] shape) {
Expand All @@ -111,6 +113,7 @@ public double[][] randomNormal2D(int[] shape) {

/**
* Generate a 3D array of random samples from the normal distribution
* @param shape The dimension of the 3D array to generate. Must have only 3 values.
* @return double[][][] 3D array of random samples from the normal distribution
*/
public double[][][] randomNormal3D(int[] shape) {
Expand Down Expand Up @@ -138,6 +141,7 @@ public double randomDoubleSample() {

/**
* Generate a 1D array of random samples between 0.0 and 1.0
* @param shape The dimension of the 1D array to generate. Must have only 1 value.
* @return double[] 1D array of random decimals
*/
public double[] randomDouble1D(int[] shape) {
Expand All @@ -153,6 +157,7 @@ public double[] randomDouble1D(int[] shape) {

/**
* Generate a 2D array of random samples between 0.0 and 1.0
* @param shape The dimension of the 2D array to generate. Must have only 2 values.
* @return double[][] 2D array of random decimals
*/
public double[][] randomDouble2D(int[] shape) {
Expand All @@ -170,6 +175,7 @@ public double[][] randomDouble2D(int[] shape) {

/**
* Generate a 3D array of random samples between 0.0 and 1.0
* @param shape The dimension of the 3D array to generate. Must have only 3 values.
* @return double[][][] 3D array of random decimals
*/
public double[][][] randomDouble3D(int[] shape) {
Expand Down Expand Up @@ -213,6 +219,7 @@ public int randomIntSample(int lower_bound, int upper_bound) {

/**
* Generate a 1D array of random integer between 0 and the upper bound
* @param shape The dimension of the 1D array to generate. Must have only 1 value.
* @param upper_bound The maximum number up to which the integers can be generated
* @return int[] 1D array of random integers
*/
Expand All @@ -230,6 +237,7 @@ public int[] randomInt1D(int[] shape, int upper_bound) {

/**
* Generate a 1D array of random integer between the lower bound and the upper bound
* @param shape The dimension of the 1D array to generate. Must have only 1 value.
* @param lower_bound The minimum number from which the integers can be generated
* @param upper_bound The maximum number up to which the integers can be generated
* @return int[] 1D array of random integers
Expand All @@ -251,6 +259,7 @@ public int[] randomInt1D(int[] shape, int lower_bound, int upper_bound) {

/**
* Generate a 2D array of random integer between 0 and the upper bound
* @param shape The dimension of the 2D array to generate. Must have only 2 values.
* @param upper_bound The maximum number up to which the integers can be generated
* @return int[][] 2D array of random integers
*/
Expand All @@ -270,6 +279,7 @@ public int[][] randomInt2D(int[] shape, int upper_bound) {

/**
* Generate a 2D array of random integer between the lower bound and the upper bound
* @param shape The dimension of the 2D array to generate. Must have only 2 values.
* @param lower_bound The minimum number from which the integers can be generated
* @param upper_bound The maximum number up to which the integers can be generated
* @return int[][] 2D array of random integers
Expand All @@ -293,6 +303,7 @@ public int[][] randomInt2D(int[] shape, int lower_bound, int upper_bound) {

/**
* Generate a 3D array of random integer between 0 and the upper bound
* @param shape The dimension of the 3D array to generate. Must have only 3 values.
* @param upper_bound The maximum number up to which the integers can be generated
* @return int[][][] 3D array of random integers
*/
Expand All @@ -314,6 +325,7 @@ public int[][][] randomInt3D(int[] shape, int upper_bound) {

/**
* Generate a 3D array of random integer between the lower bound and the upper bound
* @param shape The dimension of the 3D array to generate. Must have only 3 values.
* @param lower_bound The minimum number from which the integers can be generated
* @param upper_bound The maximum number up to which the integers can be generated
* @return int[][][] 3D array of random integers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
* @author Sambit Paul
*/

/**
* A collection of utilities
*
* @author Sambit Paul
* @version 1.0
*/
public class UtilMethods {

/**
Expand Down
49 changes: 39 additions & 10 deletions src/main/java/com/github/psambit9791/jdsp/signal/peaks/Peak.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,21 @@ public class Peak {

private double relative_height;

public Peak(double[] s, int[] m, int[] l, int[] r, String mode) {
/**
* This constructor initialises the prerequisites required to use Peak class. relative_height is set to 0.5 by default.
* @param signal The signal to be processed
* @param m List of midpoints
* @param l List of left edges
* @param r List of right edges
* @param mode mode can be one of 'peak' or 'trough'
* @throws java.lang.IllegalArgumentException if mode is not peak or trough
*/
public Peak(double[] signal, int[] m, int[] l, int[] r, String mode) throws IllegalArgumentException {
if (!mode.equals("peak") && !mode.equals("trough")) {
throw new IllegalArgumentException("mode must be peak or trough");
}

this.signal = s;
this.signal = signal;

// Peak Information
this.midpoints = m;
Expand All @@ -54,10 +66,10 @@ public Peak(double[] s, int[] m, int[] l, int[] r, String mode) {

for (int i=0; i<m.length; i++) {
if (mode.equals("peak")) {
this.height[i] = s[this.midpoints[i]];
this.height[i] = signal[this.midpoints[i]];
}
else if (mode.equals("trough")) {
this.height[i] = 0 - s[this.midpoints[i]];
else {
this.height[i] = 0 - signal[this.midpoints[i]];
}
this.plateau_size[i] = Math.abs(r[i] - l[i] + 1);
}
Expand All @@ -81,9 +93,26 @@ else if (mode.equals("trough")) {
this.width = widthData[0];
}

public Peak(double[] s, int[] m, int[] l, int[] r, double rel_height, String mode) {
/**
* This constructor initialises the prerequisites required to use Peak class.
* @param signal The signal to be processed
* @param m List of midpoints
* @param l List of left edges
* @param r List of right edges
* @param rel_height relative height at which the peak width is measured as a percentage of its prominence.
* @param mode mode can be one of 'peak' or 'trough'
* @throws java.lang.IllegalArgumentException if mode is not peak or trough
* @throws java.lang.IllegalArgumentException if rel_height is not between 0.0 and 1.0
*/
public Peak(double[] signal, int[] m, int[] l, int[] r, double rel_height, String mode) throws IllegalArgumentException {
if (!mode.equals("peak") && !mode.equals("trough")) {
throw new IllegalArgumentException("mode must be peak or trough");
}
if ((rel_height < 0) || (rel_height>1)) {
throw new IllegalArgumentException("rel_height should be between 0.0 and 1.0");
}

this.signal = s;
this.signal = signal;

// Peak Information
this.midpoints = m;
Expand All @@ -96,10 +125,10 @@ public Peak(double[] s, int[] m, int[] l, int[] r, double rel_height, String mod

for (int i=0; i<m.length; i++) {
if (mode.equals("peak")) {
this.height[i] = s[this.midpoints[i]];
this.height[i] = signal[this.midpoints[i]];
}
else if (mode.equals("trough")) {
this.height[i] = 0 - s[this.midpoints[i]];
this.height[i] = 0 - signal[this.midpoints[i]];
}
this.plateau_size[i] = Math.abs(r[i] - l[i] + 1);
}
Expand All @@ -118,7 +147,7 @@ else if (mode.equals("trough")) {

// Peak Width Information (Equivalent to scipy.signal.find_peaks() width parameter)
// Refer to https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.peak_widths.html
this.relative_height = 0.5;
this.relative_height = rel_height;
this.widthData = this.findPeakWidth(this.midpoints, this.relative_height);
this.width = widthData[0];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ public class Spike {
private double[] max_spike;
private double[] min_spike;

/**
* This constructor initialises the prerequisites required to use Peak class.
* @param signal The signal to be processed
* @param peaks The detected peaks
* @param left The troughs on the left (corresponding to the peak indices)
* @param right The troughs on the right (corresponding to the peak indices)
*/
public Spike(double[] signal, int[] peaks, int[] left, int[] right) {
this.signal = signal;
this.peaks = peaks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public void transform() {
}

/**
* Gets the length of the input signal.
* Returns the length of the input signal.
*
* @return int The updated length of the input signal.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public DiscreteFourier(double[] s) {
}

/**
* Return the length of the modified signal (padded length)
* Returns the length of the modified signal (padded length)
* @return int The modified length of the input signal
*/
public int getSignalLength() {
return this.signal.length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public void transform() {
}

/**
* Gets the length of the input signal.
* Returns the length of the input signal.
*
* @return int The updated length of the input signal.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public double[] getOutput() throws ExceptionInInitializerError {
}

/**
* Gets the length of the input signal after preprocessing for FastSine (padded to nearest power of 2).
* Returns the length of the input signal after preprocessing for FastSine (padded to nearest power of 2).
*
* @return int The updated length of the input signal.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ private void extendSignal() {
}

/**
* Return the length of the modified signal (padded length)
* Returns the length of the modified signal (padded length)
* @return int The modified length of the input signal
*/
public int getSignalLength() {
return this.signal.length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public double[] getOutput() throws ExceptionInInitializerError {
}

/**
* Gets the length of the input signal after preprocessing for FastSine (padded to nearest power of 2).
* Returns the length of the input signal after preprocessing for FastSine (padded to nearest power of 2).
*
* @return int The updated length of the input signal.
*/
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/github/psambit9791/jdsp/transform/ICA.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public ICA(double[][] signal, String func, String whiten, double[][] w_init, int
* @param func The functional form of the G function used in the approximation to neg-entropy. Can be "logcosh", "exp" or "cube".
* @param whiten Specifies the whitening strategy. Can be one of "unit-variance", "arbitrary-variance" or empty string.
* @param max_iter Maximum number of iterations during fit.
* @param tol A positive scalar giving the tolerance at which the un-mixing matrix is considered to have converged. Defaults to 1E-4.
* @param alpha G Function argument - only used in case of logcosh.
* @param random_state Random seed to initialise w_init.
*/
Expand Down Expand Up @@ -278,11 +279,12 @@ public ICA(double[][] signal, String func) {
/**
* This constructor initialises the prerequisites required to use ICA.
* @param signal Multi-dimensional signal to be transformed. Dimension 1: Samples, Dimension 2: Channels
* @param random_state Random seed to initialise w_init.
*/
public ICA(double[][] signal, long seed) {
public ICA(double[][] signal, long random_state) {
this.signal = signal;
this.components = this.signal[0].length;
this.seed = seed;
this.seed = random_state;
Random r1 = new Random(this.seed);
this.w_init = r1.randomNormal2D(new int[] {this.components, this.components});
}
Expand Down
Loading

0 comments on commit 317e398

Please sign in to comment.