title: SVD - Matrix transformation Python
body: <p>Trying to compute SVD in Python to find the most significant elements of a spectrum and created a matrix just containing the most significant parts. </p>
  
  <p>In python I have:</p>
  
  <pre><code>u,s,v = linalg.svd(Pxx, full_matrices=True)
  </code></pre>
  
  <p>This gives 3 matrices back; where "s" contains the magnitudes that corresponds to u, v.</p>
  
  <p>In order to construct a new matrix, containing all of the significant parts of the signal, I need to capture the highest values in "s" and match them with the columns in "u" and "v" and the resulting matrix should give me the most significant part of the data. </p>
  
  <p>The problem is I don't know how I would do this in Python, for example, how do I find the highest numbers in "s" and select the columns in "u" and "v" in order to create a new matrix?</p>
  
  <p>(I'm new to Python and numpy) so any help would be greatly appreciated </p>
  
  <p>Edit:</p>
  
  <pre><code>import wave, struct, numpy as np, matplotlib.mlab as mlab, pylab as pl
  from scipy import linalg, mat, dot;
  def wavToArr(wavefile):
      w = wave.open(wavefile,"rb")
      p = w.getparams()
      s = w.readframes(p[3])
      w.close()
      sd = np.fromstring(s, np.int16)
      return sd,p
  
  def wavToSpec(wavefile,log=False,norm=False):
      wavArr,wavParams = wavToArr(wavefile)
      print wavParams
      return  mlab.specgram(wavArr, NFFT=256,Fs=wavParams[2],detrend=mlab.detrend_mean,window=mlab.window_hanning,noverlap=128,sides='onesided',scale_by_freq=True)
  
  wavArr,wavParams = wavToArr("wavBat1.wav")
  
  Pxx, freqs, bins = wavToSpec("wavBat1.wav")
  Pxx += 0.0001
  
  U, s, Vh = linalg.svd(Pxx, full_matrices=True)
  assert np.allclose(Pxx, np.dot(U, np.dot(np.diag(s), Vh)))
  
  s[2:] = 0
  new_a = np.dot(U, np.dot(np.diag(s), Vh))
  print(new_a)
  </code></pre>