Advertisement
Guest User

Upsampling Interpolation Illustration

a guest
Apr 19th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.43 KB | None | 0 0
  1. pkg load signal
  2.  
  3. % big values take too long for unoptimized calculation, so
  4. % for illustration, we will use 440 instead of 44100.
  5.  
  6. Fs = 44; % the sampling frequency
  7.  
  8. Fdisp = 64*Fs; % resolution of the continuous plot
  9. F = 8*Fs; % the upsampling frequency
  10. T = 1/Fs; % the sampling time
  11.  
  12. max = 5;  % generate and calculate from 0 to here
  13. tdisp = 0:1/Fdisp:max; % the time-samples for "continuous" plotting
  14. t = 0:1/F:max; % the time-samples for upsampled plotting
  15. ts = 0:T:max;  % the time at the sample points
  16.  
  17. hz = 20; % the sine wave will be at this frequency
  18.          % this must be < Fs/2 to avoid aliasing
  19. x = @(t) sin(2*pi*hz*t);
  20. xn = x(ts);  % the discrete sequence
  21.  
  22. % perform interpolation from xn to x using sinc:
  23. interpolatedSinc = 0;
  24. for n=1:length(ts)
  25.     % n-1 due to the 1-based indexing of matlab
  26.     interpolatedSinc = interpolatedSinc + xn(n) * sinc((t-(n-1)*T)/T);
  27. end
  28.  
  29. # using zero order hold (that is, repeating the samples)
  30. #  (truncated end to match t length)
  31. interpolatedZOH =  repmat(xn(:)', F/Fs)(1:(length(xn)-1)*F/Fs+1);
  32.  
  33. # Connect-the-dots: Draw a line between 2 adjacent samples
  34. interpolatedLine =  interp1(ts,xn,t);
  35.  
  36.  
  37. graphT = 0.2; % plot this much time from the center
  38.  
  39. % the sine wave with the samples
  40. subplot(4,1,1);
  41. hold off;
  42. plot(tdisp, x(tdisp), 'k'); hold on;  % plot the sine wave
  43. stem(ts, xn);  hold on; % plot sampled
  44. xlim([max/2-graphT/2,max/2+graphT/2]); % pick graphT from the middle
  45.                                        %   (bec the edges are not bw-limited)
  46. ylim([-1,1])
  47.  
  48. % using sinc function
  49. subplot(4,1,2);
  50. hold off;
  51. stem(ts, xn);  hold on; % plot sampled
  52. stem(t, interpolatedSinc, 'k:.'); % plot interpolated in black
  53. xlim([max/2-graphT/2,max/2+graphT/2]); % pick graphT from the middle
  54.                                        %   (bec the edges are not bw-limited)
  55. ylim([-1,1])
  56.                              
  57. % using ZOH
  58. subplot(4,1,3);
  59. hold off;
  60. stem(ts, xn);  hold on; % plot sampled
  61. stem(t, interpolatedZOH, 'k:.');
  62. xlim([max/2-graphT/2,max/2+graphT/2]); % pick graphT from the middle
  63.                                        %   (bec the edges are not bw-limited)
  64. ylim([-1,1])
  65.  
  66.  
  67. % using connect-the-dots
  68. subplot(4,1,4);
  69. hold off;
  70. stem(ts, xn);  hold on; % plot sampled
  71. stem(t, interpolatedLine, 'k:.');
  72. xlim([max/2-graphT/2,max/2+graphT/2]); % pick graphT from the middle
  73.                                        %   (bec the edges are not bw-limited)
  74. ylim([-1,1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement