![JAR search and dependency download from the Maven repository](/logo.png)
com.github.psambit9791.jdsp.signal.Generate Maven / Gradle / Ivy
Show all versions of jdsp Show documentation
/*
* Copyright (c) 2020 Sambit Paul
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.github.psambit9791.jdsp.signal;
import com.github.psambit9791.jdsp.misc.UtilMethods;
import java.util.Arrays;
/**
* Generate Periodic Signal
* The GeneratePeriodic class implements methods to generate sin(), cos(), and square() wave based on sampling frequency and wave frequency
*
*
* @author Sambit Paul
* @version 1.0
*/
public class Generate {
private int Fs;
private double[] time;
/**
* This constructor initialises the prerequisites
* required to generate different signals.
* @param start The starting timepoint
* @param stop The ending timepoint
* @param samplingFreq Sampling Frequency
*/
public Generate(int start, int stop, int samplingFreq) {
this.Fs = samplingFreq;
this.time = UtilMethods.linspace(start, stop, samplingFreq, true);
}
/**
* Returns the array of time instants based on the sampling frequency
* @return double[] Time points generated
*/
public double[] getTimeArray() {
return this.time;
}
/**
* Generates a sine wave based on the provided parameters
* @param waveFreq Frequency of the wave to be generated
* @return double[] Sine wave
*/
public double[] generateSineWave(int waveFreq) {
double[] sine = new double[this.time.length];
for (int i=0; i= this.time.length) {
throw new IllegalArgumentException("Time must not be more than time length");
}
double[] imp = new double[this.time.length];
Arrays.fill(imp, 0);
imp[index] = 1;
return imp;
}
/**
* Generates a sawtooth wave based on the provided parameters
* @param waveFreq Frequency of the wave to be generated
* @param width Determines the shape of the sawtooth
* @throws java.lang.IllegalArgumentException If width value is less than 0 or greater than 1
* @return double[] Sawtooth signal
*/
public double[] generateSawtooth(int waveFreq, double width) {
if (width<0 || width>1) {
throw new IllegalArgumentException("Width must be between 0 and 1");
}
double[] sawtooth = new double[this.time.length];
double[] t = new double[this.time.length];
double[] tmod = new double[this.time.length];
for (int i=0; i