import java.util.Arrays;
public class Convolution {
public static void main(String[] args){
System.out.println(Arrays.toString(getLinearlySpaceArray(-3.0, 3.0, 0.01)));
double[] xwerte = getLinearlySpaceArray(-3.0, 3.0, 0.01);
double[] ywerte = applyRectFct(xwerte);
double[] y2werte = applyLinFct(xwerte);
PlotHelper.plot2D(xwerte, ywerte);
PlotHelper.plot2D(xwerte, y2werte );
PlotHelper.plot2D(shift(xwerte, ywerte, 1.0), shift(ywerte, xwerte, 0));
PlotHelper.plot2D(multiply(xwerte, xwerte), multiply(ywerte, ywerte));
PlotHelper.plot2D(mirror(xwerte), mirror(ywerte));
System.out.println(integrate(xwerte, ywerte));
PlotHelper.plot2D(xwerte, ywerte, applyLinFct(ywerte), convolution(xwerte, ywerte, ywerte));
}
public static double[] getLinearlySpaceArray(double start, double end, double spacing){
int laenge = (int) ((end - start)/spacing) +1;
double Array[] = new double[laenge];
for(int i = 0; i < laenge; i++){
Array[i] = start;
start += spacing;
}
return Array;
}
public static double rectangularFunction(double input){
if(input < 0){
input *= -1;
}
if(input <= 1){
input = 1.0;
}
else{
input = 0.0;
}
return input;
}
public static double truncatedLinearFunction(double input){
if(input < 0){
input *= -1;
}
if(input <=1){
input= -0.5 + input + 0.5;
}
else{
input = 0.0;
}
return input;
}
public static double[] applyRectFct(double[] array){
double[] y = new double[array.length];
for(int i = 0; i < y.length; i++){
y[i] = rectangularFunction(array[i]);
}
return y;
}
public static double[] applyLinFct(double[] array){
double[] y = new double[array.length];
for(int i = 0; i < y.length; i++){
y[i] = truncatedLinearFunction(array[i]);
}
return y;
}
public static double[] shift(double[] ys, int entries){
double[] newArray = new double[ys.length];
for(int i = 0; i < ys.length; i++){
if(entries > 0){
ys[i] = ys[i-entries];
}
else{
ys[i] = ys[i+entries];
}
}
return newArray;
}
public static double getSpacing(double[] array){
double wert = array[0] - array[1];
return wert;
}
public static double[] shift(double[] array1, double[] array2, double value ){
double[] newArray = new double [array1.length];
for(int i = 0; i < newArray.length; i++){
newArray[i] = array1[i]+value;
}
return newArray;
}
public static double[] multiply(double[] ys1, double[] ys2){
double[] array = new double[ys1.length];
if(ys1.length == ys2.length){
for(int i = 0; i < ys1.length; i++){
array[i] = ys1[i] * ys2[i];
}
}
else{
System.out.println("Konnte nicht multipliziert werden, weil die Anzahl der "
+ "beiden Arrays unterschiedlich ist.");
System.exit(1);
}
return array;
}
public static double[] mirror(double[] ys1){
double[] array = new double[ys1.length];
for(int i = 0; i < ys1.length; i++){
array[i] = ys1[(ys1.length-1)-i];
}
return array;
}
public static double integrate(double[] xs, double[] ys){
double Flächenintegral = 0;
for(int i = 0; i < xs.length-1; i++){
Flächenintegral += (xs[i+1] - xs[i])*((ys[i]+ys[i+1])/2);
}
return Flächenintegral;
}
public static double[] convolution(double[] xs, double[] ys1, double[] ys2){
double[] array = new double [xs.length];
int x0 = 0;
for(int i = 0 ; i < xs.length; i++){
array[i] = mirror(ys1[i]);
array[i] = shift(ys1[i], x0);
array[i] = multiply(ys1[i], ys2[i]);
array[i] = integrate(ys1[i], ys2[i]);
}
return array;
}
}