% Written by: Daniel Llamocca (2013)
% TIME SCALING of discrete time signals

clear all; % clears any previously-generated variables
close all; % close all windows
clc; % clears MATLAB command screen

% Generating vector of samples:
  n = 0:59; % [0 1 2 3 4 5 .... 59]
  % On purpose, we picked n >= 0. We could have include negative values of
  % 'n'. It's a matter of choice.
  
% Creating the input signal x[n]:
  x = sin(n/10); % [sin(0) sin(1/10) sin (2/10)... sin (59/10)]
  % Note: 'sin' assumes the input is given in radians: e.g.: 1/10 radian
  %       If we do 'sin(n)', we will get [sin(0) sin(1) sin (2) ... sin(59)]
  %       This is sampling the sinusoid at 1 radian. This is not good enough!
  %       (You can try for yourself and it will not be a good sinusoid
  %       approximation)
  %       To get a good approximation of a sinusoid, we can decrease the
  %       sampling period to (for example) 1/10 of a radian, i.e. picking
  %       values from the sinusoid every 1/10 or a radian. 
  %       When we plot sin(n/10), we get a fairly good sinusoid approximation

% Time scaling:
% We have x[n], now we want y[n] = x[k*n]
  k = -2; % k = 2; k = 0.5, k = -0.5
  y = sin((n*k/10)); % 'n' is replaced by 'n*k'

% plot: (x axis, y axis)
 figure; plot (n, x, '.b'); title ('x[n]');
 figure; plot (n, y, '.b'); title ('y[n] = x[k*n]');

% Stem plot (more useful in some instances)
 figure; stem (n, x, '.b'); title ('x[n]');
 figure; stem (n, y, '.b'); title ('y[n] = x[k*n]');
