Jiachen-Zhang
3/1/2020 - 2:28 AM

信号与系统lab1

% -------------------------------------------------------------------------
% 1.4 Properties of Discrete-Time Systems
% Basic Problem (a) Linear
% -------------------------------------------------------------------------
clear;

n = -10:10;
x_1 = n==0;
x_2 = 2*(n==0);
x_3 = x_1 + x_2;

figure(1)

% plot x[n]
subplot(2, 1, 1)
stem(n, x_1, 'r', 'diamondr'); hold on;
stem(n, x_2, 'b', 'square'); hold on;
stem(n, x_3, 'g', '<'); hold on;
legend('$x_1[n]$', '$x_2[n]$', '$x_3[n]$', 'Interpreter','latex')
axis([-10, 10, -2, 4]);

% plot y[n]
subplot(2, 1, 2)
y_1 = sin(pi/2*x_1);
y_2 = sin(pi/2*x_2);
y_3 = sin(pi/2*x_3);
stem(n, y_1, 'r', 'diamondr'); hold on;
stem(n, y_2, 'b', 'square'); hold on;
stem(n, y_3, 'g', '<'); hold on;
stem(n, y_1 + y_2, 'y', '+')
% As y_1 + y_2 ~= y_3, this system is not linearity

legend('$y_1[n]$', '$y_2[n]$', '$y_3[n]$', '$y_1[n] + y_2[n]$', 'Interpreter','latex')
axis([-10, 10, -2, 2]);

% -------------------------------------------------------------------------
% 1.4 Properties of Discrete-Time Systems
% Basic Problem (b) Causal
% -------------------------------------------------------------------------
clc; close all; clear;
% system y[n] = x[n] + x[n + 1]
% Input
n_1 = -5:9;
x = n_1 >= 0;
stem(n_1, x, '<-'); hold on;
n_2 = -6:9
y = (n_2 >= 0) + ((n_2 + 1) >= 0);
stem(n_2, y, '>--'); hold on;
legend('$x[n]$', '$y[n]$', 'Interpreter','latex')
axis([-7, 11, -1, 2]);
% -------------------------------------------------------------------------
% The second system y[n]=x[n] + x[n+1] is non-causal. 
% This system returns a sumation of the current input signal and a time-advanced version of the input signal.
% This means, that for example at the output time n=0, 
% the system requires access to the value of the input signal at time n=0 and at time n=1 
% Clearly, this is impossible in a realizable system, as nobody can look into the future
% -------------------------------------------------------------------------

% -------------------------------------------------------------------------
% 1.4 Properties of Discrete-Time Systems
% Intermediate Problems (c) Stable
% -------------------------------------------------------------------------
clc; close all; clear;
% define the domain vector as n  ranging between -2 and 3.
n = -3:3
x = abs(n)
y = log(x)
stem(n, x, 'b>--'); hold on;
stem(n, y, 'r<--');

legend('Input $x[n]$', 'Output $y[n]$', 'Interpreter','latex')
axis([-4, 4, -1, 4]);
xlabel('n')
% -------------------------------------------------------------------------
% The value of y[n] at n = 0 disapear
% -------------------------------------------------------------------------

% -------------------------------------------------------------------------
% 1.4 Properties of Discrete-Time Systems
% Intermediate Problems (d) Invertible
% System y[n] = sin(pi/2 * x[n])
% -------------------------------------------------------------------------
clc; close all; clear;
% define the domain vector as n  ranging between -2 and 3.
n = -5:5;
x = n
y = sin(pi/2 * x)
stem(n, x, 'b>--'); hold on;
stem(n, y, 'r<--');

legend('Input $x[n]$', 'Output $y[n]$', 'Interpreter','latex', 'Location','northwest');
axis([-5, 5, -5, 5]);
xlabel('n')
% -------------------------------------------------------------------------
% Different input signals corresponds to same output signal, not invertible
% -------------------------------------------------------------------------

% -------------------------------------------------------------------------
% 1.4 Properties of Discrete-Time Systems
% Advanced Problems (e) Invertible
% System y[n] = x^3[n] 
% => is: time-invariant, causal, stable, invertible
% => is not: linear
% linear, time-invariant, causal, stable, invertible
% -------------------------------------------------------------------------
clc; close all; clear;
% define the domain vector as n  ranging between -2 and 3.
n = -3:3;
x_1 = abs(n);
x_2 = n;
x_3 = x_1 + x_2;

y_1 = x_1.^3;
y_2 = x_2.^3;
y_3 = x_3.^3;

subplot(1, 2, 1)
stem(n, x_1, '<--'); hold on;
stem(n, x_2, '>--'), hold on;
stem(n, x_3, 'o--'), hold on;
legend('$x_1[n]$', '$x_2[n]$', '$x_3[n]$', 'Interpreter','latex', 'Location','southeast');
axis([-3, 3, -3, 3]);

subplot(1, 2, 2)
stem(n, y_3); hold on;
stem(n, y_1 + y_2), hold on;

axis([-3, 3, -250, 250]);
legend('$y_3[n]$', '$y_1[n] + y_2[n]$', 'Interpreter','latex', 'Location','southeast');

% -------------------------------------------------------------------------
% y_3[n] ~= y_1[n] + y_2[n], not linear
% -------------------------------------------------------------------------

% -------------------------------------------------------------------------
% 1.4 Properties of Discrete-Time Systems
% Advanced Problems (f) Invertible
% System y[n] = nx[n] 
% => is: linear, causal, stable, invertible
% => is not: time-invariant
% linear, time-invariant, causal, stable, invertible
% -------------------------------------------------------------------------
clc; close all; clear;
% define the domain vector as n  ranging between -2 and 3.
n = -3:3;
x_1 = abs(n);
x_2 = abs(n-1);

y_1 = n.*x_1;
y_1_shift = (n-1).*x_2;
y_2 = n.*x_2;

subplot(1, 2, 1)
stem(n, x_1, '<--'); hold on;
stem(n, x_2, '>--'), hold on;
legend('$x_1[n]$', '$x_2[n]$', 'Interpreter','latex', 'Location','southeast');
axis([-3, 3, -3, 4]);

subplot(1, 2, 2)
stem(n, y_1_shift, '<--'); hold on;
stem(n, y_2, '>--'), hold on;
legend('$y_{1shift}[n]$', '$y_2[n]$', 'Interpreter','latex', 'Location','southeast');
axis([-3, 3, -16, 6]);
% -------------------------------------------------------------------------
% y_1_shift[n] ~= y_2[n], not time-invariant
% -------------------------------------------------------------------------

% -------------------------------------------------------------------------
% 1.4 Properties of Discrete-Time Systems
% Advanced Problems (f) Invertible
% System y[n] = x[2n] 
% => is: linear, time-invariant, stable
% => is not: causal, invertible
% linear, time-invariant, causal, stable, invertible
% -------------------------------------------------------------------------
clc; close all; clear;
% define the domain vector as n  ranging between -2 and 3.
n = 0:5;
x_1 = x(n);
y_1 = y(n);
stem(n, x_1, 'b>--'); hold on;
stem(n, y_1, 'r<--');
legend('Input $x_1[n]$', 'Output $y_1[n]$', 'Interpreter','latex', 'Location','northwest');
function val = x(n)
    val = abs(n);
end
function val = y(n)
    val = x(2*n);
end
% -------------------------------------------------------------------------
% The system y[n] = x[2n] is non-causal. 
% This system may returns a time-advanced version of the input signal.
% This means, that for example at the output time n=1, 
% the system requires access to the value of the input signal at time n=2 
% Clearly, this is impossible in a realizable system, as nobody can look into the future
% 
% The system y[n] = x[2n] is not invertible
% for any n is even, we can get x[n] = y[n/2],
% but for n is odd, we could not get x[n] from y[m], where m is an integer
% -------------------------------------------------------------------------

% -------------------------------------------------------------------------
% 1.4 Implementing a First-Order Difference Equation
% Advanced Problems (a) (b)
% -------------------------------------------------------------------------
clc; close all; clear;
a = 1;
yn1 = 0;
n = 0:30;
% unit impulse
x_1 = [1 zeros(1, 30)];
y_1 = diffenq(a, x_1, yn1);
% unit step
x_2 = ones(1, 31);
y_2 = diffenq(a, x_2, yn1);

subplot(2, 1, 1)
stem(n, y_1, 'b>--');
legend('$y_1[n]$', 'Interpreter','latex', 'Location','northwest');
subplot(2, 1, 2)
stem(n, y_2, 'r<--');
legend('$y_2[n]$', 'Interpreter','latex', 'Location','northwest');
% need to describe the start and end value of each output

function y = diffenq(a, x, yn1)
    if isempty(x)
        y = [];
        return
    end
    y = zeros(1, length(x));
    y(1) = a*yn1 + x(1);
    for idx=2:length(x)
       y(idx) = a*y(idx-1) + x(idx);
    end
end

% -------------------------------------------------------------------------
% 1.4 Implementing a First-Order Difference Equation
% Advanced Problems (c)
% -------------------------------------------------------------------------
clc; close all; clear;
a = 1;
yn1 = -1;
n = 0:30;
% unit step
x_1 = ones(1, 31);
y_1 = diffenq(a, x_1, yn1);
% 2 * unit step
x_2 = 2*ones(1, 31);
y_2 = diffenq(a, x_2, yn1);

stem(n, y_1, 'b>--'); hold on;
stem(n, y_2, 'r<--'); hold on;
stem(n, 2*y_1 - y_2, 'go--');
legend('$y_1[n]$', '$y_2[n]$', '$2y_1[n]-y_2[n]$', 'Interpreter','latex', 'Location','northwest');
% need to describe the start and end value of each output

function y = diffenq(a, x, yn1)
    if isempty(x)
        y = [];
        return
    end
    y = zeros(1, length(x));
    y(1) = a*yn1 + x(1);
    for idx=2:length(x)
       y(idx) = a*y(idx-1) + x(idx);
    end
end
% -------------------------------------------------------------------------
% The difference should be $2y_1[n-1] - y_2[n-1]$ = $yn1$ = -1
% -------------------------------------------------------------------------