Guest User

Untitled

a guest
Mar 22nd, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. import java.awt.Dimension;
  2. import javax.swing.JFrame;
  3. import org.jfree.chart.ChartFactory;
  4. import org.jfree.chart.ChartPanel;
  5. import org.jfree.chart.JFreeChart;
  6. import org.jfree.data.xy.XYDataset;
  7. import org.jfree.data.xy.XYSeries;
  8. import org.jfree.data.xy.XYSeriesCollection;
  9.  
  10. /**
  11. * Description:
  12. * Attempt to convolve two 1D functions (original signal with response function ) using FFT, and then restore origin signal.
  13. *
  14. *
  15. *
  16. * Dependencies:
  17. * FFT.java ( source: https://introcs.cs.princeton.edu/java/97data/FFT.java.html )
  18. * Complex.java ( source: https://introcs.cs.princeton.edu/java/97data/Complex.java.html )
  19. * JFreeChart (for visualizing results)
  20. *
  21. *
  22. * **/
  23.  
  24. public class FFTConvolutionTest {
  25.  
  26. public static final int nfft = 1024; //FFT length
  27.  
  28. public static void main(String[] args) {
  29.  
  30. //A first function for convolution : original signal is a sine curve
  31. double samplingFrequency = 150; //Hz
  32. double timeLength = 4; //sec
  33. double signalFrequency = 5; //Hz
  34.  
  35. int samples = (int) (timeLength * samplingFrequency + 1);
  36.  
  37. double[] signal = new double[samples];
  38. for (int i = 0; i < signal.length; i++) {
  39. signal[i] = Math.sin(2 * Math.PI * ((double) i / samplingFrequency) * signalFrequency);
  40. }
  41. visualizeArray(signal, 1d/samplingFrequency, "signal");
  42.  
  43. //A second function for convolution : response function is a gaussian distribution
  44. double length = 1;
  45. double step = length/samplingFrequency;
  46.  
  47.  
  48. double[] response = new double[samples];
  49. double center = (int) (samples / 2);
  50. double stdDev = 0.3;
  51. double factor = 1/(Math.sqrt(2 * Math.PI * Math.pow(stdDev, 2)));
  52. double exponent;
  53. for (int i = 0; i < response.length; i++) {
  54. exponent = (double) - 1/2 * ( Math.pow(((double) i - center) * step /stdDev, 2));
  55. response[i] = Math.pow(Math.E, exponent) * factor;
  56. }
  57. visualizeArray(response, step, "response");
  58.  
  59.  
  60. //Making convolution
  61. double[] conv = convolution(signal, response);
  62. visualizeArray(conv, 1d/samplingFrequency, "convolved");
  63.  
  64. }
  65.  
  66. public static double[] convolution(double[] xd, double[] yd) {
  67.  
  68. // fft length must be larger than both arrays lengths
  69. if ( (xd.length > nfft) || (yd.length > nfft)) {
  70. throw new IllegalArgumentException("nnft must be larger: " + xd.length + ", " + yd.length);
  71. }
  72.  
  73. // Forming complex versions of arrays
  74. Complex[] x = new Complex[nfft];
  75. Complex[] y = new Complex[nfft];
  76.  
  77. // Extending both x and y arrays to fft length, completing rests with zeros.
  78. for (int i=0; i< nfft; i++) {
  79.  
  80. if (i < xd.length) {
  81. x[i] = new Complex(xd[i], 0);
  82. } else {
  83. x[i] = new Complex(0, 0);
  84. }
  85.  
  86. if (i < yd.length) {
  87. y[i] = new Complex(yd[i], 0);
  88. } else {
  89. y[i] = new Complex(0, 0);
  90. }
  91.  
  92. }
  93.  
  94. // Doing the convolution
  95. Complex[] c = FFT.convolve(x, y);
  96.  
  97. //returning real part of convolution
  98. double[] cd = new double[nfft];
  99. for (int i=0; i < cd.length; i++) {
  100. cd[i] = c[i].re();
  101. }
  102. return cd;
  103. }
  104.  
  105. public static void visualizeArray(double[] array, double step, String name) {
  106.  
  107. double[][] array2D = new double[array.length][2];
  108. for (int i=0; i<array2D.length; i++){
  109. array2D[i][0] = (double) i * step;
  110. array2D[i][1] = array[i];
  111. }
  112.  
  113. final XYSeriesCollection xyCollection = new XYSeriesCollection();
  114.  
  115. XYDataset xyDataset = xyCollection;
  116. XYSeries series = createSeries(array2D, name);
  117. xyCollection.addSeries(series);
  118. JFreeChart chart = ChartFactory.createXYLineChart(name, "x", "y", xyDataset);
  119. ChartPanel chartPanel = new ChartPanel(chart, true, true, true, false, true);
  120.  
  121. JFrame frame = new JFrame();
  122. frame.add(chartPanel);
  123. frame.setPreferredSize(new Dimension(700, 500));
  124. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  125. frame.pack();
  126. frame.setLocationRelativeTo(null); // center the frame
  127. frame.setVisible(true);
  128. }
  129.  
  130. public static XYSeries createSeries(double[][] array, String name){
  131.  
  132. XYSeries series = new XYSeries(name);
  133. int[][] xyValues = new int[array.length][2];
  134. for (int i = 0; i < xyValues.length; i++) {
  135. series.add(array[i][0], array[i][1]);
  136. }
  137. return series;
  138. }
  139.  
  140. }
Add Comment
Please, Sign In to add comment