I've been working on a project where I needed to implement convolution and correlation methods using two formulas, which I did. Then I need to define the following method for printing f, correlation, and convolution side-by-side (which is the part I need help figuring out):
Convolution Formula:
(f * g)[n] = ∑ f[n + (M - 1) - m] * g[m]
where f is an array of size N, g is an array of size M, and f*g is the array of size N - M + 1 that stores the result of the convolution.
Correlation Formula:
(f ** g)[n] = ∑ f[n + m] * g[m]
where f is an array of size N, g is an array of size M, and f**g is the array of size N - M + 1 that stores the result of the correlation.
Define the following method for printing f, f*g, f**g side-by-side:
private static void print(double[] f, double[] convolution, double[] correlation)
This is what I have. Everything in main is driver code to test the program. I need help with the print method:
public static void main(String[] args) {
double[] f = new double[18];
for (int i = 0; i < f.length; i++) {
f[i] = i;
}
double[] g1 = {0.25, 0.25, 0.5},
g2 = {0.1, 0.2, 0.3, 0.4};
print(f, convolution(f, g1), correlation(f, g1));
print(f, convolution(f, g2), correlation(f, g2));
for (int i = 0; i < f.length; i++) {
f[i] = Math.sin(i);
}
print(f, convolution (f, g1), correlation(f, g1));
print(f, convolution (f, g2), correlation(f, g2));
}
**private static void print(double[] f, double[] convolution, double[] correlation) {
System.out.println("i\tf(i)\tconvolution[i]\tcorrelation[i]");
for (int i = 0; i < convolution.length; i++) {
System.out.println(i + "\t" + (f[i]) + "\t" + (convolution[i]) + "\t"
+ correlation[i] + "\t");
}
}**
private static double[] convolution (double[] f, double[] g) {
int n = f.length - g.length + 1; //N - M + 1
double[] result2 = new double[n]; //create new array and define bounds
for (int i = 0; i < n; ++i) { //outer loop executes n times
for (int j = 0; j < g.length; ++j) { //inner loop executes up to the length of g
result2[i] += f[i + (g.length - 1) - j] * g[j]; //convolution calculation
}
}
return result2;
}
private static double[] correlation (double[] f, double[] g) {
int n = f.length - g.length + 1; // N - M + 1
double[] result = new double[n]; // create new array and define bounds
for (int i = 0; i < n; ++i) { // outer loop executes n times
for (int j = 0; j < g.length; ++j) { //inner loop executes up to the length of g
result[i] += f[i + j] * g[j]; // correlation calculation
}
}
return result;
}
}
This is an example of what the output should look like. This is the last printing as per the calls in the main method. I didn't want to include too much info:
This is what I'm getting. I'm not really concerned about the formatting.
I can't seem to get i to print 17 times and I'm unsure as to how to get it to print "----" for the empty values. Do I use an if statement? If I change the for loop in the print method to this:
for (int i = 0; i < f.length; i++) {
System.out.println(i + "\t" + (f[i]) + "\t" + (convolution[i]) + "\t"
+ correlation[i] + "\t");