title: RTL SDR IQ AM Demodulation
body: <p>The project i am working now currently is tuning to PMR frequencies and saving audio to a WAV file, however when
  i receive IQ samples from SDR device, and demodulate  to AM - i hear noise only.</p>
  
  <p>Edit, here is the RAW IQ written to WAV file:
  <a href="https://voca.ro/iSRELps4JBg" rel="nofollow noreferrer">https://voca.ro/iSRELps4JBg</a></p>
  
  <p>For demodulation i used this approach:</p>
  
  <p><a href="https://github.com/simonyiszk/minidemod/blob/master/minidemod-wfm-atan.c" rel="nofollow noreferrer">https://github.com/simonyiszk/minidemod/blob/master/minidemod-wfm-atan.c</a></p>
  
  <p>However the sound seems to be noise...</p>
  
  <pre><code>  while(get_samples(buffer, buffer_size) == 1)
      {
  
  
          // Demodulate to AM
          uint8_t uiAMBuff[buffer_size];
  
          double i1=0, i2, q1=0, q2;
          short s; // 2 bytes
          double sum=0;
          double dbAmp=0;
          for (int i = 0; i &lt; buffer_size; i+=2)
          {
              // I / Q
  
              i2=((unsigned char)buffer[i]-127);
              q2=((unsigned char)buffer[i+1]-127);
  
  
              double phi1=get_phase(i1,q1);
              double phi2=get_phase(i2,q2);
  
              double dphi=phi2-phi1;
              if(dphi&lt;-PI) dphi+=2*PI; //dphi below -180°? Let's interpret it in the other direction!
              if(dphi&gt;PI) dphi-=2*PI;  //dphi above 180°? Let's interpret it in the other direction!
  
              // Now let's use signed 16 bit output rather than 8 bit unsigned.
              s=((SHRT_MAX-1)/PI)*dphi; //Okay so let's get the actual sample. dphi should be between -PI and +PI.
  
              // Store AM in the buffer that gets written to a WAV file
              uiAMBuff[i]=s;
              uiAMBuff[i+1]=s&gt;&gt;8;
  
              i1=i2;
              q1=q2;
              // This is to calculate DB
              double y = s/32768.0;
              sum += y * y; 
  
          }
  </code></pre>
  
  <p>How to properly demodulate IQ to AM please?</p>