title: Matlab: conv() -> fft() * fft() -> ifft()
body: <p>Good day to everyone!</p>
  
  <p>I tried to tackle the basic problem of obtaining the original signal from observing its convolution with some known impulse response. </p>
  
  <p>But the results I get are somehow totally wrong, and probably I have a mix of different wrong steps here. I've already looked through similar topics here and on other sites like developpez, but failed to figure out the cause. I would appreciate any help. </p>
  
  <p>Let's say my true signal <em>f</em>[.] is just an impulse at the time 1, and the impulse response <em>g</em>[.] is Gaussian. I compute their convolution <em>h</em>[.] by <code>conv()</code>, and then, basically, want to find <code>ifft( fft[h]./fft[g] )</code>, expecting this to be <em>f</em>[.]. </p>
  
  <p>The first problem is that <code>conv()</code> makes an array of the <em>n+m-1</em> elements, where <em>n,m</em> are the lengths of the argument arrays. So, to perform <code>fft[h]./fft[g]</code> I need to do smth with the length of g. It's the first suspicious place where I may act wrong (see the code). What's the right way to do it?</p>
  
  <p>The second problem is that I get something very different from what the initial true signal.</p>
  
  <p>The third problem is that I can't understand how to takle the signal shifts. In matlab, I have to operate with positive-time signals, but, for example, the gaussian impulse response has both time-negative and time-positive elements, so, to work with it here, I need to shift it 'forward' (the peek will move to the right), and than I need to 'de-shift' the result?</p>
  
  <p>Thanks!</p>
  
  <p>Here's my crap on that :)</p>
  
  <pre><code>close all;
  
  TrueSignal = zeros( 101, 1 ); % impulse in t = 1.
  TrueSignal( 1 ) = 1;
  ImpulseResp = normpdf(-1:0.02:1)/normpdf( 0 ); % 101 elements array
  
  figure;
  subplot( 2,2,1 );
  title('True signal')
  plot( TrueSignal );
  subplot( 2,2,2 );
  title('Impulse response')
  plot( ImpulseResp );
  
  Conv = conv( TrueSignal, ImpulseResp ); % produces 201 elements array.
  subplot( 2,2,3 );
  title('Convolution')
  plot( Conv );
  
  % Wrong? I need a 201 elements array to represent the impulse response.
  ImpulseResp_sparse = normpdf( -1:0.01:1 )/normpdf( 0 );
  FIR = fft( ImpulseResp_sparse )/201;
  
  Inverse = ifft( fft( Conv )./FIR ); % UPD Added fft() according to one of comments, bad mistake, but still not preventing.
  
  subplot( 2,2,4 );
  title('What is that???')
  plot( abs( Inverse ) ); % It's weird! With no abs(), result is even more weird! 
  </code></pre>