Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pkg load signal
- % big values take too long for unoptimized calculation, so
- % for illustration, we will use 440 instead of 44100.
- Fs = 44; % the sampling frequency
- Fdisp = 64*Fs; % resolution of the continuous plot
- F = 8*Fs; % the upsampling frequency
- T = 1/Fs; % the sampling time
- max = 5; % generate and calculate from 0 to here
- tdisp = 0:1/Fdisp:max; % the time-samples for "continuous" plotting
- t = 0:1/F:max; % the time-samples for upsampled plotting
- ts = 0:T:max; % the time at the sample points
- hz = 20; % the sine wave will be at this frequency
- % this must be < Fs/2 to avoid aliasing
- x = @(t) sin(2*pi*hz*t);
- xn = x(ts); % the discrete sequence
- % perform interpolation from xn to x using sinc:
- interpolatedSinc = 0;
- for n=1:length(ts)
- % n-1 due to the 1-based indexing of matlab
- interpolatedSinc = interpolatedSinc + xn(n) * sinc((t-(n-1)*T)/T);
- end
- # using zero order hold (that is, repeating the samples)
- # (truncated end to match t length)
- interpolatedZOH = repmat(xn(:)', F/Fs)(1:(length(xn)-1)*F/Fs+1);
- # Connect-the-dots: Draw a line between 2 adjacent samples
- interpolatedLine = interp1(ts,xn,t);
- graphT = 0.2; % plot this much time from the center
- % the sine wave with the samples
- subplot(4,1,1);
- hold off;
- plot(tdisp, x(tdisp), 'k'); hold on; % plot the sine wave
- stem(ts, xn); hold on; % plot sampled
- xlim([max/2-graphT/2,max/2+graphT/2]); % pick graphT from the middle
- % (bec the edges are not bw-limited)
- ylim([-1,1])
- % using sinc function
- subplot(4,1,2);
- hold off;
- stem(ts, xn); hold on; % plot sampled
- stem(t, interpolatedSinc, 'k:.'); % plot interpolated in black
- xlim([max/2-graphT/2,max/2+graphT/2]); % pick graphT from the middle
- % (bec the edges are not bw-limited)
- ylim([-1,1])
- % using ZOH
- subplot(4,1,3);
- hold off;
- stem(ts, xn); hold on; % plot sampled
- stem(t, interpolatedZOH, 'k:.');
- xlim([max/2-graphT/2,max/2+graphT/2]); % pick graphT from the middle
- % (bec the edges are not bw-limited)
- ylim([-1,1])
- % using connect-the-dots
- subplot(4,1,4);
- hold off;
- stem(ts, xn); hold on; % plot sampled
- stem(t, interpolatedLine, 'k:.');
- xlim([max/2-graphT/2,max/2+graphT/2]); % pick graphT from the middle
- % (bec the edges are not bw-limited)
- ylim([-1,1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement