title: Python: Reconstruct audio file from STFT
body: <p>As a simple experiment, I want to compute the <a href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.stft.html" rel="nofollow noreferrer">stft</a> of an audio file:</p>
  
  <pre><code>sample_rate, samples = wav.read(file)
  
  f, t, Zxx = stft(samples, sample_rate)
  _, reconstructed = istft(Zxx, sample_rate)
  padded_samples = np.zeros_like(reconstructed)
  padded_samples[:len(samples)] = samples
  print (np.sum(padded_samples - reconstructed))
  </code></pre>
  
  <p>Output: <code>-1.37309940428</code>. Pretty small, isn't it? Given that <code>samples</code> is of shape <code>(9218368,)</code>.</p>
  
  <pre><code>test_file = os.path.join(temp_folder, 'reconstructed.wav')
  wav.write(test_file, sample_rate, reconstructed)
  </code></pre>
  
  <p>The reconstructed file sounds terrible. The original is barely heareble underneath the noise. Have I made a mistake, or is it simply impossible to recover an audio file from the STFT?</p>
  
  <p>Do you have any other suggestions on how to convert an audio file to some kind of processable data and then reconstruct it from that? What other kind of data structures can be used to process audio files?</p>
  
  <p>Thank you.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>As suggested by Warren:</p>
  
  <pre><code>print (samples.shape)
  print (samples.dtype)
  print (reconstructed.dtype)
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>(9218368,)
  int16
  float64
  </code></pre>
  
  <p>According to the scipy docs int and float input have different meaning when writing a wav file. I tried casting reconstructed to np.int16:</p>
  
  <pre><code>rounded_reconstructed = np.rint(reconstructed).astype(np.int16)
  
  test_file = os.path.join(temp_folder, 'reconstructed.wav')
  wav.write(test_file, sample_rate, rounded_reconstructed)
  </code></pre>
  
  <p>The result is barely distinguishable from the original. Thank you for the help.</p>