title: How do I compare two audio signals with different sampling frequencies in Matlab?
body: <p>I'm trying to better understand upsampling and downsampling, and am using an audio signal to do so. I start with a base sampling rate of 66.15kHz, upsample by 2, run it through an LPF (sample rate of 66.15kHz, cutoff frequency of half that), then downsample by 2.</p>
  
  <p>To visualize the differences between the initial and final signal, I created two spectrograms, with different sampling frequencies. There's lots of clear similarities, as well as a tighter band around the signals in the lower frequency version (as a result of the upsampling?), but I think the best thing to do is just take some sort of subtraction of the images. The problem is that the matrices are different sizes, so I can't use approaches found <a href="https://stackoverflow.com/questions/9443854/how-to-do-subtraction-between-2-spectrogram-and-then-draw-a-new-one-in-matlab">here</a> or <a href="https://stackoverflow.com/questions/22298644/how-to-compare-two-audio-signals-with-matlab">here</a>.</p>
  
  <p>Should I just upsample both of them to be the same size? Will the differences still be there?</p>
  
  <p>The spectrograms in question:</p>
  
  <p><a href="https://i.stack.imgur.com/PBrdw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PBrdw.png" alt="Spectrograms"></a></p>
  
  <p>Here's the code I used, some parts are commented out that may help to visualize things along the way:</p>
  
  <pre><code>%clc, clear all, close all;
  
  NFFT = 1:10:4000; % used like F in the spectrogram fxn
  WINDOW = 2000;
  NOVERLAP = 0;
  
  preupsample = audioread('audioclip.wav');
  Fo = 66150;
  orig_nyq = Fo/2;
  U = 2;
  D = 3;
  postupsample = upsample(preupsample, U);
  
  [preLPF, ~, W] = myfft(postupsample,U*Fo,length(postupsample));
  
  % figure, plot(W,mag2db(abs(preLPF))), xlabel('Frequency (Hz)'), ylabel('Magnitude Response (dB)');
  % title('Magnitude Response after upsampling');
  
  filterorder = 100;
  Fpass = orig_nyq;
  Fs = U*Fo;
  Rpass = 0.00057565; % .1dB passband ripple
  Rstop = 1e-4; % -80dB stopband attenuation
  LPF = firceqrip(filterorder,Fpass/(Fs/2),[Rpass Rstop],'passedge');
  % fvtool(LPF,'Fs',Fs,'Color','White');
  
  filtered_upsample = filter(LPF,1,postupsample);
  [postLPF, ~, W] = myfft(filtered_upsample,U*Fo,length(filtered_upsample));
  % figure, plot(W,mag2db(abs(postLPF))), xlabel('Frequency (Hz)'), ylabel('Magnitude Response (dB)');
  % title('Magnitude Response after applying LPF');
  
  postdownsample = downsample(filtered_upsample,D);
  [postDS, w, W] = myfft(postdownsample,U*Fo/D,length(postdownsample));
  % figure, plot(W,mag2db(abs(postDS))), xlabel('Frequency (Hz)'), ylabel('Magnitude Response (dB)');
  % title('Magnitude Response after downsampling');
  
  %[~,F1,T1,P1]=
  figure, spectrogram(preupsample(:,1),WINDOW,NOVERLAP,NFFT,Fo,'yaxis');
  %[~,F2,T2,P2]=
  figure, spectrogram(postdownsample(:,1),WINDOW,NOVERLAP,NFFT,Fo*U/D,'yaxis');
  % surf(T2,F2,10*(log10(abs(P2))-log10(abs(P1))),'edgecolor','none');
  % view(90,-90);
  % axis tight;
  </code></pre>
  
  <p>The contents of myfft.m:</p>
  
  <pre><code>function [X, w, W] = myfft(x, F, N)
      X = fftshift(fft(x, N));
      w = fftshift((0:N-1)/N*2*pi);
      w(1:N/2) = w(1:N/2) - 2*pi;
      W = F*w;
  end
  </code></pre>