Convolution
eharetea

This java class emulates a signal that can be convolved with other signals in discrete time.

Convolution with JAVA.

Date Created:Friday March 06th, 2009 12:20 AM
Date Modified:Tuesday April 27th, 2010 11:41 PM

import java.io.*;

public class Signal{

 private int length; 
 private double[] x;

 public Signal(double[] a) {
   x = a;
   length = a.length;
 }

 public Signal(int size) {
   length = size;
   x = new double[size];
 }

 public void set(int n, double value) {
   x[n] = value;
 }
 
 public double get(int n) { return x[n]; }

 public Signal conv(double[] h) {
  Signal a = new Signal(x.length + h.length - 1);
  for (int i = 0; i < length; i++) {
    for (int j=0; j < h.length; j++) {
      a.set(i+j, a.get(i+j) + x[i] * h[j]);
    }
  }
  return a;
 }

 public String toString() {
   String l = "[";
   for( double val:x ) {
    l += " " + val;
   }
   return l + "]";
 }

 public static void main(String[] args) {
   double[] x1 = {1,0,2};
   double[] h = {2,3,1};
   Signal x = new Signal(x1);
   Signal y = x.conv(h);
   System.out.println(y.toString());
 }
} 

            


Downloads:
Download: convolution.java 871 B

Please login or Click Here to register for downloads
Creative Commons License
Convolution by Dan Lynch
is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
Based on a work at www.3daet.com
Permissions beyond the scope of this license may be available at http://www.3daet.com