title: How to recreate an instrument sound from a .WAV file by using FFT and findpeaks() in MATLAB?
body: <p>I want to generate my own samples of a Kick, Clap, Snare and Hi-Hat sounds in MATLAB based on a sample I have in .WAV format. </p>
  
  <p>Right now it does not sound at all correct, and I was wondering if my code does not make sense? Or if it is that I am missing some sound theory.</p>
  
  <p>Here is my code right now. </p>
  
  <pre><code> [y,fs]=audioread('cp01.wav');
   Length_audio=length(y);
   df=fs/Length_audio;
   frequency_audio=-fs/2:df:fs/2-df;
   frequency_audio = frequency_audio/(fs/2); //Normalize the frequency
   figure
   FFT_audio_in=fftshift(fft(y))/length(fft(y));
   plot(frequency_audio,abs(FFT_audio_in));
  </code></pre>
  
  <p>The original plot of y.</p>
  
  <p><a href="https://i.stack.imgur.com/63ANR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/63ANR.png" alt="The sound "></a></p>
  
  <p>My FFT of y</p>
  
  <p><a href="https://i.stack.imgur.com/XP5VB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XP5VB.png" alt="enter image description here"></a></p>
  
  <p>I am using the findpeaks() function to find the peaks of the FFT with amplitude greater than 0.001.</p>
  
  <pre><code>[pk, loc] = findpeaks(abs(FFT_audio_in), 'MinPeakHeight', 0.001);
  </code></pre>
  
  <p>I then find the corresponding normalized frequencies from the frequency audio (positive ones) and the corresponding peak.</p>
  
  <pre><code> loc = frequency_audio(loc);
   loc = loc(length(loc)/2+1:length(loc))
   pk = pk(length(pk)/2+1:length(pk))
  </code></pre>
  
  <p>So the one sided, normalized FFT looks like this.</p>
  
  <p><a href="https://i.stack.imgur.com/uD4yU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uD4yU.png" alt="enter image description here"></a></p>
  
  <p>Since it looks like the FFT, I think I should be able to recreate the sound by summing up sinusoids with the correct amplitude and frequency. Since the clap sound had 21166 data points I use this for the for loop.</p>
  
  <pre><code>for i=1:21116
  
      clap(i) = 0;
  for j = 1:length(loc);
      clap(i) = bass(i) + pk(j)*sin(loc(j)*i);
  
  end
  
  end
  </code></pre>
  
  <p>But this results in the following sound, which is nowhere near the original sound. 
  <a href="https://i.stack.imgur.com/7TuyY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7TuyY.png" alt="enter image description here"></a></p>
  
  <p>What should I do differently?</p>