I am writing a piece of code which shows the epidemic model SEIR works. I am fairly new to programming in Java, and i've found this piece of code online, however i cannot get it running for some reason. Any help would be appreciated!!!
package seir_code;
import java.util.ArrayList;
import java.util.List;
public class seir_project {
public static void main(String[] args) {
int totalDays = 160; // How many days/times to loop
int N = 1000; // Population
int I0 = 1; // Starting infected/exposed
double beta = 0.2; // Infection rate
double gamma = 1.0/10.0; // recovery time (days to the -1)
double a = 1.0/2.0; // incubation period (days to the -1)
List<Double> S = new ArrayList<>();
List<Double> E = new ArrayList<>();
List<Double> I = new ArrayList<>();
List<Double> R = new ArrayList<>();
private void createData() {
final int R0 = 0;
final int S0 = N - E0 - R0;
S.add((double) S0);
E.add((double) I0);
I.add(0.0);
R.add(0.0);
for (int day = 1; day < totalDays + 1; day++) {
double[] derivative = deriv(day);
S.add(derivative[0]);
E.add(derivative[1]);
I.add(derivative[2]);
R.add(derivative[3]);
}
}
private double[] deriv(int day) {
day = day - 1;
double dS = (beta * S.get(day) * I.get(day)) / N;
double newS = S.get(day) - (dS);
double newE = E.get(day) + (dS - (a * E.get(day)));
double newI = I.get(day) + ((a * E.get(day)) - (gamma * I.get(day)));
double newR = R.get(day) + (gamma * I.get(day));
return new double[] {newS, newE, newI, newR};
}
}
}
Tried to get running to return an output but code showing errors
Related
this is a continuation of this post. I must calculate many times some statistics(Max, mean, min, median and std dev) of arrays and I have a performance issue given the sort of my arrays in the method calcMaxMinMedian.
Given, I could not improve much further the summary statistics of an array performance. I am trying now to understand strategies and work arounds to parallelize my upper calls or any other smart thoughts.
I have seen this doc but I am not familiar
As well as this (post)[https://stackoverflow.com/questions/20375176/should-i-always-use-a-parallel-stream-when-possible/20375622].
I tried using parallel streams, however probably given my SharedResource, the actual performance using the for loop was worse.
Time (s) functionUsingForLoop173
Time (s) functionUsingParallelStream194
Do anyone have an idea of what could I try to parallelize or any other thoughts on how to improve the overrall performance?
Here is what I tried:
public class MaxMinMedianArrayUtils {
int[] sharedStaticResource={1,5,5};//Shared resource across
/**
* Return an array with summary statistics. Max, mean,std dev,median,min.
* Throw an IllegalArgumentException if array is empty.
*
* #param a array.
* #return array returning Max(0), mean(1),std dev(2),median(3),min(4) in
* respective
* positions.
*/
public static double[] getSummaryStatistics(double[] a) {
double[] summary = new double[5];
if (a.length == 0) {
throw new IllegalArgumentException(
"Array is empty, please " + "verify" + " the values.");
} else if (a.length == 1) {
summary[0] = a[0];
summary[1] = a[0];
summary[2] = 0;
summary[3] = a[0];
summary[4] = a[0];
} else {
double[] meandStd = calcMeanSDSample(a);
summary[1] = meandStd[0];//Mean
summary[2] = meandStd[1];//Standard Deviation
double[] maxMinMedian = calcMaxMinMedian(a);
summary[0] = maxMinMedian[0];//Max
summary[4] = maxMinMedian[1];//Min
summary[3] = maxMinMedian[2];//Median
}
return summary;
}
public static double[] calcMeanSDSample(double numArray[]) {
int length = numArray.length;
double[] meanStd = new double[2];
if (length == 0) {
throw new IllegalArgumentException(
"Array is empty, please " + "verify" + " the values.");
} else if (length == 1) {
meanStd[0] = numArray[0];
meanStd[1] = 0.0;
} else {
double sum = 0.0, standardDeviation = 0.0;
for (double num : numArray) {
sum += num;
}
meanStd[0] = sum / length;
for (double num : numArray) {
standardDeviation += Math.pow(num - meanStd[0], 2);
}
meanStd[1] =
Math.sqrt(standardDeviation / ((double) length - 1.0));//-1
// because it is
// for sample
}
return meanStd;
}
public static double[] calcMaxMinMedian(double[] a) {
double[] maxMinMedian = new double[3];
if (a.length == 0) {
throw new IllegalArgumentException(
"Array is empty, please " + "verify" + " the values.");
} else if (a.length == 1) {
for (int i = 0; i < 3; i++) {
maxMinMedian[i] = a[0];
}
} else {
Arrays.sort(a);
maxMinMedian[0] = a[a.length - 1];
maxMinMedian[1] = a[0];
maxMinMedian[2] = (a.length % 2 != 0) ? (double) (a[a.length / 2]) :
(double) ((a[(a.length - 1) / 2] + a[a.length / 2]) / 2.0);
}
return maxMinMedian;
}
public static void main(String[] args) {
int numVals = 1000;
// double[] ar = new double[numVals];
int numCalculations = 2 * 1000 * 1 * 1000;
// int numCalculations = 2 * 1000;
MaxMinMedianArrayUtils maxMinMedianArrayUtils=
new MaxMinMedianArrayUtils();
Instant start = Instant.now();
double[][] statsPerCalculation=
maxMinMedianArrayUtils.functionUsingForLoop(numVals,
numCalculations);
Instant end = Instant.now();
long totalTime = Duration.between(start, end).toSeconds();
System.out.println("Time (s) functionUsingForLoop" + totalTime);
Instant start3 = Instant.now();
double[][] statsPerCalculation3=
maxMinMedianArrayUtils.functionUsingParallelStream(numVals,
numCalculations);
Instant end3 = Instant.now();
long totalTime3 = Duration.between(start3, end3).toSeconds();
System.out.println("Time (s) functionUsingParallelStream" + totalTime3);
}
private double[][] functionUsingForLoop(int numVals,
int numCalculations) {
// calculations that is used to get some values, but is not modified.
double[][] statsPerCalculation= new double[numCalculations][5];//Each
// line
// stores
// the stats of the array generated in the numCalculations loop
for (int i = 0; i < numCalculations; i++) {//Complete independent
// calculations that I want to parallelize
double[]array=functionSimulateCalculations(numVals);
double[] stats = getSummaryStatistics(array);
for(int s = 0; s < stats.length; s++) {//Copy
statsPerCalculation[i][s] = stats[s];
}
}
return statsPerCalculation;
}
private double[][] functionUsingParallelStream(int numVals,
int numCalculations) {
// calculations that is used to get some values, but is not modified.
double[][] statsPerCalculation= new double[numCalculations][5];//Each
// line
// stores
// the stats of the array generated in the numCalculations loop
double[][] finalStatsPerCalculation = statsPerCalculation;
IntStream.range(0,numCalculations).parallel().forEach((i)->{
double[] array=functionSimulateCalculations(numVals);
double[] stats = getSummaryStatistics(array);
for(int s = 0; s < stats.length; s++) {
finalStatsPerCalculation[i][s] = stats[s];
}
}
);
return statsPerCalculation;
}
private double[] functionSimulateCalculations(int numVals) {
double[] ar=new double[numVals];
for (int k = 0; k < numVals; k++) {//To simulate the
// actual function of my
// use case
ar[k] = Math.random()*sharedStaticResource[0];
}
return ar;
}
} // Utility
Part of your issue is that you are computing your randomised the data samples inside the tests, but have contention with the singleton random number generation in parallel threads. Also this means that you have no means of validating that the parallel algorithm matches the serial results.
Refactor your tests so that inputs are pre-computed once - you don't care about timing this step:
private double[][] generateInputData(int numVals, int numCalculations) {
// calculations that is used to get some values, but is not modified.
double[][] inputs = new double[numCalculations][];// Each
for (int i = 0; i < numCalculations; i++) {
inputs[i] = functionSimulateCalculations(numVals);
}
return inputs;
}
Then you can run both tests on same inputs:
private double[][] functionUsingForLoop(double[][]arrays) {
// calculations that is used to get some values, but is not modified.
int numCalculations = arrays.length;
double[][] statsPerCalculation = new double[numCalculations][5];// Each
for (int i = 0; i < numCalculations; i++) {
double[] stats = getSummaryStatistics(arrays[i]);
for (int s = 0; s < stats.length; s++) {
statsPerCalculation[i][s] = stats[s];
}
}
return statsPerCalculation;
}
private double[][] functionUsingParallelStream(double[][]arrays) {
int numCalculations = arrays.length;
double[][] statsPerCalculation = new double[numCalculations][5];// Each
double[][] finalStatsPerCalculation = statsPerCalculation;
IntStream.range(0, numCalculations).parallel().forEach((i) -> {
double[] stats = getSummaryStatistics(arrays[i]);
for (int s = 0; s < stats.length; s++) {
finalStatsPerCalculation[i][s] = stats[s];
}
});
return statsPerCalculation;
}
Finally make your main() do some warmups, initialise the arrays, time each section and compare the results:
for (int numCalculations : new int[] { 1, 2, 8, 8, 2 * 1000, 2* 10000, 2*1000*1*1000 } ) {
double[][] arrays = maxMinMedianArrayUtils.generateInputData(numVals, numCalculations);
// ...
double[][] statsPerCalculation= maxMinMedianArrayUtils.functionUsingForLoop(arrays);
// ...
double[][] statsPerCalculation3= maxMinMedianArrayUtils.functionUsingParallelStream(arrays);
// ...
// COMPARE the results
if (statsPerCalculation3.length != statsPerCalculation.length)
throw new RuntimeException("Results not same!");
for (int i = statsPerCalculation3.length - 1; i >= 0; i--) {
// System.out.println("statsPerCalculation ["+i+"]="+Arrays.toString(statsPerCalculation[i]));
// System.out.println("statsPerCalculation3["+i+"]="+Arrays.toString(statsPerCalculation3[i]));
for (int v = statsPerCalculation3[i].length - 1; v >= 0; v--) {
if (Math.abs(statsPerCalculation3[i][v]-statsPerCalculation[i][v]) > 0.0000000000001)
throw new RuntimeException("Results not same at ["+i+"]["+v+"]");
}
}
}
At this point, you'll see quite different trend in the results, parallel stream version a lot quicker than non-parallel.
I have a big problem. I try to create a neural network and want to train it with a backpropagation algorithm. I found this tutorial here http://mattmazur.com/2015/03/17/a-step-by-step-backpropagation-example/ and tried to recreate it in Java. And when I use the training data he uses, I get the same results as him.
Without backpropagation my TotalError is nearly the same as his. And when I use the back backpropagation 10 000 time like him, than I get the nearly the same error. But he uses 2 Input Neurons, 2 Hidden Neurons and 2 Outputs but I'd like to use this neural network for OCR, so I need definitely more Neurons. But if I use for example 49 Input Neurons, 49 Hidden Neurons and 2 Output Neurons, It takes very long to change the weights to get a small error. (I believe it takes forever.....). I have a learningRate of 0.5. In the constructor of my network, I generate the neurons and give them the same training data like the one in the tutorial and for testing it with more neurons, I gave them random weights, inputs and targets. So can't I use this for many Neurons, does it takes just very long or is something wrong with my code ? Shall I increase the learning rate, the bias or the start weight?
Hopefully you can help me.
package de.Marcel.NeuralNetwork;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Random;
public class Network {
private ArrayList<Neuron> inputUnit, hiddenUnit, outputUnit;
private double[] inHiWeigth, hiOutWeigth;
private double hiddenBias, outputBias;
private double learningRate;
public Network(double learningRate) {
this.inputUnit = new ArrayList<Neuron>();
this.hiddenUnit = new ArrayList<Neuron>();
this.outputUnit = new ArrayList<Neuron>();
this.learningRate = learningRate;
generateNeurons(2,2,2);
calculateTotalNetInputForHiddenUnit();
calculateTotalNetInputForOutputUnit();
}
public double calcuteLateTotalError () {
double e = 0;
for(Neuron n : outputUnit) {
e += 0.5 * Math.pow(Math.max(n.getTarget(), n.getOutput()) - Math.min(n.getTarget(), n.getOutput()), 2.0);
}
return e;
}
private void generateNeurons(int input, int hidden, int output) {
// generate inputNeurons
for (int i = 0; i < input; i++) {
Neuron neuron = new Neuron();
// for testing give each neuron an input
if(i == 0) {
neuron.setInput(0.05d);
} else if(i == 1) {
neuron.setOutput(0.10d);
}
inputUnit.add(neuron);
}
// generate hiddenNeurons
for (int i = 0; i < hidden; i++) {
Neuron neuron = new Neuron();
hiddenUnit.add(neuron);
}
// generate outputNeurons
for (int i = 0; i < output; i++) {
Neuron neuron = new Neuron();
if(i == 0) {
neuron.setTarget(0.01d);
} else if(i == 1) {
neuron.setTarget(0.99d);
}
outputUnit.add(neuron);
}
// generate Bias
hiddenBias = 0.35;
outputBias = 0.6;
// generate connections
double startWeigth = 0.15;
// generate inHiWeigths
inHiWeigth = new double[inputUnit.size() * hiddenUnit.size()];
for (int i = 0; i < inputUnit.size() * hiddenUnit.size(); i += hiddenUnit.size()) {
for (int x = 0; x < hiddenUnit.size(); x++) {
int z = i + x;
inHiWeigth[z] = round(startWeigth, 2, BigDecimal.ROUND_HALF_UP);
startWeigth += 0.05;
}
}
// generate hiOutWeigths
hiOutWeigth = new double[hiddenUnit.size() * outputUnit.size()];
startWeigth += 0.05;
for (int i = 0; i < hiddenUnit.size() * outputUnit.size(); i += outputUnit.size()) {
for (int x = 0; x < outputUnit.size(); x++) {
int z = i + x;
hiOutWeigth[z] = round(startWeigth, 2, BigDecimal.ROUND_HALF_UP);
startWeigth += 0.05;
}
}
}
private double round(double unrounded, int precision, int roundingMode)
{
BigDecimal bd = new BigDecimal(unrounded);
BigDecimal rounded = bd.setScale(precision, roundingMode);
return rounded.doubleValue();
}
private void calculateTotalNetInputForHiddenUnit() {
// calculate totalnetinput for each hidden neuron
for (int s = 0; s < hiddenUnit.size(); s++) {
double net = 0;
int x = (inHiWeigth.length / inputUnit.size());
// calculate toAdd
for (int i = 0; i < x; i++) {
int v = i + s * x;
double weigth = inHiWeigth[v];
double toAdd = weigth * inputUnit.get(i).getInput();
net += toAdd;
}
// add bias
net += hiddenBias * 1;
net = net *-1;
double output = (1.0 / (1.0 + (double)Math.exp(net)));
hiddenUnit.get(s).setOutput(output);
}
}
private void calculateTotalNetInputForOutputUnit() {
// calculate totalnetinput for each hidden neuron
for (int s = 0; s < outputUnit.size(); s++) {
double net = 0;
int x = (hiOutWeigth.length / hiddenUnit.size());
// calculate toAdd
for (int i = 0; i < x; i++) {
int v = i + s * x;
double weigth = hiOutWeigth[v];
double outputOfH = hiddenUnit.get(s).getOutput();
double toAdd = weigth * outputOfH;
net += toAdd;
}
// add bias
net += outputBias * 1;
net = net *-1;
double output = (double) (1.0 / (1.0 + Math.exp(net)));
outputUnit.get(s).setOutput(output);
}
}
private void backPropagate() {
// calculate ouputNeuron weigthChanges
double[] oldWeigthsHiOut = hiOutWeigth;
double[] newWeights = new double[hiOutWeigth.length];
for (int i = 0; i < hiddenUnit.size(); i += 1) {
double together = 0;
double[] newOuts = new double[hiddenUnit.size()];
for (int x = 0; x < outputUnit.size(); x++) {
int z = x * hiddenUnit.size() + i;
double weigth = oldWeigthsHiOut[z];
double target = outputUnit.get(x).getTarget();
double output = outputUnit.get(x).getOutput();
double totalErrorChangeRespectOutput = -(target - output);
double partialDerivativeLogisticFunction = output * (1 - output);
double totalNetInputChangeWithRespect = hiddenUnit.get(x).getOutput();
double puttedAllTogether = totalErrorChangeRespectOutput * partialDerivativeLogisticFunction
* totalNetInputChangeWithRespect;
double weigthChange = weigth - learningRate * puttedAllTogether;
// set new weigth
newWeights[z] = weigthChange;
together += (totalErrorChangeRespectOutput * partialDerivativeLogisticFunction * weigth);
double out = hiddenUnit.get(x).getOutput();
newOuts[x] = out * (1.0 - out);
}
for (int t = 0; t < newOuts.length; t++) {
inHiWeigth[t + i] = (double) (inHiWeigth[t + i] - learningRate * (newOuts[t] * together * inputUnit.get(t).getInput()));
}
hiOutWeigth = newWeights;
}
}
}
And my Neuron Class:
package de.Marcel.NeuralNetwork;
public class Neuron {
private double input, output;
private double target;
public Neuron () {
}
public void setTarget(double target) {
this.target = target;
}
public void setInput (double input) {
this.input = input;
}
public void setOutput(double output) {
this.output = output;
}
public double getInput() {
return input;
}
public double getOutput() {
return output;
}
public double getTarget() {
return target;
}
}
Think about it: you have 10,000 propagations through 49->49->2 neurons. Between the input layer and the hidden layer, you have 49 * 49 links to propagate through, so parts of your code are being executed about 24 million times (10,000 * 49 * 49). That is going to take time. You could try 100 propogations, and see how long it takes, just to give you an idea.
There are a few things that can be done to increase performance, like using a plain array instead of an ArrayList, but this is a better topic for the Code Review site. Also, don't expect this to give drastic improvements.
Your back propagation code has complexity of O(h*o + h^2) * 10000, where h is the number of hidden neurons and o is the number of output neurons. Here's why.
You have a loop that executes for all of your hidden neurons...
for (int i = 0; i < hiddenUnit.size(); i += 1) {
... containing another loop that executes for all the output neurons...
for (int x = 0; x < outputUnit.size(); x++) {
... and an additional inner loop that executes again for all the hidden neurons...
double[] newOuts = new double[hiddenUnit.size()];
for (int t = 0; t < newOuts.length; t++) {
... and you execute all of that ten thousand times. Add on top of this O(i + h + o) [initial object creation] + O(i*h + o*h) [initial weights] + O(h*i) [calculate net inputs] + O(h*o) [calculate net outputs].
No wonder it's taking forever; your code is littered with nested loops. If you want it to go faster, factor these out - for example, combine object creation and initialization - or reduce the number of neurons. But significantly cutting the number of back propagation calls is the best way to make this run faster.
using this link, i saw an algo about the gift wrapping algorithm / jarvis march
https://www.cs.duke.edu/courses/fall14/cps130/notes/scribe14.pdf
i tried coding it but it doesnt follow the algo.
here is my main function
import java.util.Random;
import java.util.Scanner;
public class driver {
static Point[] points;
static Scanner scan = new Scanner(System.in);
static Random rand = new Random();
public static void main (String[] args)
{
System.out.println("Jarvis Algorithm Test\n");
boolean i = true;
while(i){
System.out.println("SELECT:");
System.out.println("RANDOM:");
System.out.println("USER INPUT:");
System.out.print(":");
// String comm = scan.nextLine();
String comm = "random";
comm = comm.toLowerCase();
if(comm.equals("random")){
randomize();
i = false;
}
else if(comm.equals("user input")){
userinput();
}
else{
System.out.println("INVALID");
}
System.out.println("");
}
Jarvis j = new Jarvis();
j.convexHull(points);
}
public static void randomize(){
int p = 10; //= askpoints();
points = new Point[p];
for(int i = 0; i<p; i++){
points[i] = new Point();
points[i].x = rand.nextInt(300) + 20 ;
points[i].y = rand.nextInt(300) + 20 ;
System.out.println(points[i].x+ " " +points[i].y);
}
}
public static void userinput(){
int n = askpoints();
points = new Point[n];
System.out.println("Enter "+ n +" x, y cordinates");
for (int i = 0; i < n; i++)
{
points[i] = new Point();
points[i].x = scan.nextInt();
points[i].y = scan.nextInt();
}
}
public static int askpoints(){
System.out.print("how many points:");
int i = scan.nextInt();
return i;
}
}
here is my jarvis code
/**
** Java Program to Implement Jarvis Algorithm
* http://www.sanfoundry.com/java-program-to-implement-jarvis-algorithm/
**/
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Arrays;
import javax.swing.JFrame;
/** Class point **/
class Point
{
int x, y;
}
/** Class Jarvis **/
public class Jarvis extends JFrame{
public ArrayList<Integer> x = new ArrayList<Integer>();
public ArrayList<Integer> y = new ArrayList<Integer>();
private boolean CCW(Point p, Point q, Point r)
{
// int val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
// if (val >= 0)
// return false;
// return true;
return (q.y - p.y) * (r.x - p.x) < (r.y - p.y) * (q.y - p.x);
}
public void convexHull(Point[] points){
int n = points.length;
int[] next = new int[n];
int[] prev = new int[n];
Arrays.fill(next, -1);
int l = 0;
for(int i =1; i<n;i++){
if(points[i].x < points[l].x){
l = i;
}
}
System.out.println(points[l].x);
int p = l;
int q;
do{
q = p + 1;
for(int i = 0;i<n;i++){
System.out.println(CCW(points[p],points[i],points[q]));
if( p!=i &&CCW(points[p],points[i],points[q])){
q = i;
}
next[p] = q;
prev[q] = p;
p = q;
}
}while(p == l);
}
/*
* display(points, next);
*/
}
i tried to print the values of next to know the indexes however the values are all 0. so i tried printing what CCW returns and it always returns false. why is that i followed the algorithm in the link.
any tips?
Hi guys I´m new in java.
I'm trying to convert an octave function in to Java(using JBlas) but I´m not sure how to do this line:
OCTAVE: Thetas{i} = rand(sizes(i+1), sizes(i) + 1)*2*EPSILON-EPSILON;
My code in Java:
public class InitializeThetas {
public static DoubleMatrix InitializeThetas(DoubleMatrix sizes, double epsilon)
{
int L= sizes.length;
epsilon = 0.03;
DoubleMatrix Thetas = new DoubleMatrix(new double[]{});
Random r = new Random();
for (int i = 1; i <= L - 1; i++)
{
//Thetas{i} = rand(sizes(i+1), sizes(i) + 1)*2*EPSILON-EPSILON;
Thetas.data[i]= r.nextInt() * 2 * epsilon - epsilon;
}
return Thetas;
}
}
Is this close to what you want?
public class InitializeThetas {
public static Collection<DoubleMatrix> InitializeThetas(DoubleMatrix sizes, double epsilon)
{
int L= sizes.length;
epsilon = 0.03;
Collection<DoubleMatrix> Thetas = new ArrayList<DoubleMatrix>();
for (int i = 0; i < L - 1; i++)
{
Thetas.add(DoubleMatrix.rand(Double.valueOf(sizes.get(i + 1)).intValue(), Double.valueOf(sizes.get(i)).intValue() + 1).mul(2).mul(epsilon).sub(epsilon));
}
return Thetas;
}
public static void main(String[] args) {
DoubleMatrix soze = new DoubleMatrix(10);
for(int i = 0;i<10;i++){
soze.put(i,i+1);
}
System.out.println(InitializeThetas(soze,1.1));
}
}
import java.util.*;
import java.awt.*;
import java.io.*;
public class Project4
{
public static void main (String[] args)
{
// MAIN CODE
String noteRepresentation = "78!9#a#bc$d%ef^g&A*BC(D)EF_G+H";
String noteString = SimpleInput.getString("String of notes");
// init strLen etc
int strLen = noteString.length();
int samples = (int)((.125) * (22050)); // 1/8 Times the sample per sec
int finalLen = strLen * samples;
// Create a silence array
double silenceArray[] = new double[(int)samples];
// = new double[(double)samples]; // Be able to create a new array for the silence (periods).
// Create a main sound sample
SoundSample[] ssarrFS = new SoundSample[(int)finalLen];
String noteLetter;
for ( int noteIndex = 0; noteIndex < noteString.length(); noteIndex++)
{
double freq = -1;
double amplitude = -1;
noteLetter = noteString.substring(noteIndex, noteIndex + 1);
getFreq(noteLetter);
getAmp(freq, noteLetter);
SoundSample[] tempArray = createSineWave(freq, amplitude);
if (noteRepresentation.indexOf(noteLetter) == -1)
{
//Silence
for(int index1 = 0;index1<samples;index1++)
{
int value1 = tempArray[index1].getValue();
ssarrFS[(int)((index1+(samples*(noteIndex))))].setValue(value1);
}
}
else
{
freq = -1;
amplitude = -1;
for(int index2 = 0;index2<samples;index2++)
{
getFreq(noteLetter);
getAmp(freq, noteLetter);
int value2 = tempArray[index2].getValue();
ssarrFS[(int)(index2+(samples*(noteIndex)))].setValue(value2);
}
}
}
Sound sFinal = modifySound(finalLen, ssarrFS);
sFinal.explore();
}
public static Sound modifySound(int finalLen, SoundSample[] ssarrFS)
{
Sound sFinal = new Sound( finalLen+1);
SoundSample[] ssarr3 = sFinal.getSamples();
int ind;
for (ind = 0 ; ind < finalLen ; ++ind )
{
int valueFinal = ssarrFS[ind].getValue();
// check for clipping
if ( valueFinal > 32767 )
{
valueFinal = 32767;
}
if ( valueFinal < -32768 )
{
valueFinal = -32768;
}
ssarr3[ind].setValue ( valueFinal );
}
return sFinal;
}
public static double getFreq (String noteLetter)
{
String noteRepresentation = "78!9#a#bc$d%ef^g&A*BC(D)EF_G+H";
double x = noteRepresentation.indexOf(noteLetter); // x becomes 15
double y = noteRepresentation.indexOf('H'); // y becomes 29
double exponent = (double)((x - y)/12.0);
double freq = (double)(440.0 * Math.pow (2.0, exponent)); // fr
System.out.println(freq);
return freq;
}
public static SoundSample[] createSineWave (double freq, double amplitude)
{
Sound s = new Sound ((int)((.125) * (22050)));
int samplingRate = (int)(s.getSamplingRate());
int rawValue = 0;
int value = 0;
int interval = (int)(1.0 / freq);
int samplesPerCycle = (int)(interval * samplingRate);
int maxValue =(int)( 2 * Math.PI);
SoundSample[] tempArray = s.getSamples();
int index;
for (int i = 0 ; i < s.getLength(); ++i )
{
rawValue = (int)(Math.sin ((i / samplesPerCycle) * maxValue));
value = (int) (amplitude * rawValue);
tempArray[i].setValue(value);
}
//s = null;
//System.gc();
return tempArray;
}
public static double getAmp (double freq, String noteLetter)
{
double samplesPerCycle = 22050 / freq;
double sampleIndex = 22050 / 8;
double wavePoint = sampleIndex / samplesPerCycle;
double rawSample = Math.sin (wavePoint * 2.0 * Math.PI);
double amplitude = rawSample * 20000;
System.out.println(amplitude);
return amplitude;
}
}
I am trying to combine notes that a user will input and be able to take their frequency and amplitudes and be able to create a .wav file that will output the sound. When I try to compile the current code, I keep receiving a nullpointerexception error at line
ssarrFS[(int)(index2+(samples*(noteIndex)))].setValue(value2);
I believe that I have incorrectly set up my arrays and may have caused memory leaks in my program.
You declare your array here
SoundSample[] ssarrFS = new SoundSample[(int)finalLen];
But then you never populate it's values, and you try to use it here:
ssarrFS[(int)((index1+(samples*(noteIndex))))].setValue(value1);
When you try to use it though, your array looks like this:
ssarrFS = [0] = null
[1] = null
[2] = null
...
[finalLen] = null
You attempt to call setValue on an element inside of it. Because you've not populated ssarrFS, this amounts to:
null.setValue(value1);
And because null does not refer to an object in memory, this will throw your NullPointerException.