% ddc_waveforms     - Downconvert waveforms given phase advance per sample, filter

function data = ddc_waveforms(data);

[M, N] = size(data.chan);

for k=1:N
  [I, Q] = reconstruct(data.chan(:,k), data.theta);
  baseband = filter(ones(data.nfilt, 1), data.nfilt, I - Q*i);
  data.iq(:,k) = baseband(data.nfilt:end);
end
data.t_iq = data.t(1:end-data.nfilt);

% Apply calibrations and phase offsets
ph_ref  = angle(data.iq(:,4)); % reference channel

for ch=1:3
  data.ph(:, ch) = (angle(data.iq(:, ch)) - ph_ref)/pi*180 + data.ph_offset(ch);
  data.amp(:, ch) = abs(data.iq(:, ch));
  if strcmp(data.pwrsw{ch}, 'Power')
    data.amp(:, ch) = data.amp(:,ch).^2;
  end
  data.amp(:, ch) = data.amp(:, ch) * data.scale(ch);
end
