0% found this document useful (0 votes)
5 views8 pages

Discrete Time Sinusoid Simulation

The document describes generating and plotting discrete time sinusoidal signals with different parameters: - A discrete time sinusoid x(nT) is generated by sampling a continuous time sinusoid x(t) at intervals of T=1/fs, where fs is the sampling frequency. - The frequency f0, amplitude A, phase phi, and sampling frequency fs can be varied. Increasing f0 increases the signal frequency, while changing phi only time-shifts the signal. - Plots of the continuous and discrete time signals are shown for different parameters, along with playing the discrete signal as audio to demonstrate the effect of varying f0 and A.

Uploaded by

李峻霖
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
0% found this document useful (0 votes)
5 views8 pages

Discrete Time Sinusoid Simulation

The document describes generating and plotting discrete time sinusoidal signals with different parameters: - A discrete time sinusoid x(nT) is generated by sampling a continuous time sinusoid x(t) at intervals of T=1/fs, where fs is the sampling frequency. - The frequency f0, amplitude A, phase phi, and sampling frequency fs can be varied. Increasing f0 increases the signal frequency, while changing phi only time-shifts the signal. - Plots of the continuous and discrete time signals are shown for different parameters, along with playing the discrete signal as audio to demonstrate the effect of varying f0 and A.

Uploaded by

李峻霖
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.

f0 = 1000;

total_time = 5;

fsRatio = 20;
fs = f0*fsRatio;%20000
T = 1/fs; % sampling interval in time
A = 0.25; % magnitude
phi = 0; % phase

t = (0:T:total_time); % time axis


% x(t) = A*cos(2*pi*f0*t+phi) where A: magnitude (>= 0), f0: 1/(fundamental period), phi: phase
x_CT = A*cos(2*pi*f0*(0:1/(f0*100):total_time)+phi); % x(t), sampling rate is high enough so th
x_DT = A*cos(2*pi*f0*t+phi); % x[n] = x(nT), sampled cosine/discrete time sinusoid
%Npoint = length(x_DT); % number of points in sampled cosine

sound(x_DT,fs); % play the signal by the sound card, type "help sound" (without the double quot

figure
plot((0:1/(f0*100):total_time), x_CT,'-', 'linewidth', 2); % CT signal
hold on
stem(t, x_DT,'r', 'linewidth', 2); % DT signal
plot(t, x_DT,'r', 'linewidth', 2); % connet the dots, i.e., connect DT x[n]

xlabel('Time (sec.)');
ylabel('x(nT)');
title('Discrete time sinusoid (time domain)');
axis([0 1/f0*5 -A A]); % only observe the signal from time 0 to time 1/f0*3. once you remove t
legend('x(t)', 'x[n]', 'connected x[n]')

xlim([0.00000 0.00500])
ylim([-1.00 1.00])

1
f0 = 1000; % in Hz
total_time = 5; % in sec.

% !!! Sampling in time


fsRatio = 20;
fs = f0*fsRatio;
T = 1/fs; % sampling interval in time
A = 0.5; % magnitude
phi = 0; % phase

t = (0:T:total_time); % time axis


% x(t) = A*cos(2*pi*f0*t+phi) where A: magnitude (>= 0), f0: 1/(fundamental period), phi: phase
x_CT = A*cos(2*pi*f0*(0:1/(f0*100):total_time)+phi); % x(t), sampling rate is high enough so th
x_DT = A*cos(2*pi*f0*t+phi); % x[n] = x(nT), sampled cosine/discrete time sinusoid
%Npoint = length(x_DT); % number of points in sampled cosine
sound(x_DT,fs); % play the signal by the sound card, type "help sound" (without the double quot

figure

plot((0:1/(f0*100):total_time), x_CT,'-', 'linewidth', 2); % CT signal


hold on
stem(t, x_DT,'r', 'linewidth', 2); % DT signal
plot(t, x_DT,'r', 'linewidth', 2); % connet the dots, i.e., connect DT x[n]
xlabel('Time (sec.)');
ylabel('x(nT)');
title('Discrete time sinusoid (time domain)');
2
axis([0 1/f0*5 -A A]); % only observe the signal from time 0 to time 1/f0*3. once you remove t
legend('x(t)', 'x[n]', 'connected x[n]')

f0 = 1000; % in Hz
total_time = 5; % in sec.

% !!! Sampling in time


fsRatio = 20;
fs = f0*fsRatio;
T = 1/fs; % sampling interval in time
A = 1; % magnitude
phi = 0; % phase

t = (0:T:total_time); % time axis


% x(t) = A*cos(2*pi*f0*t+phi) where A: magnitude (>= 0), f0: 1/(fundamental period), phi: phase
x_CT = A*cos(2*pi*f0*(0:1/(f0*100):total_time)+phi); % x(t), sampling rate is high enough so th
x_DT = A*cos(2*pi*f0*t+phi); % x[n] = x(nT), sampled cosine/discrete time sinusoid
%Npoint = length(x_DT); % number of points in sampled cosine
sound(x_DT,fs); % play the signal by the sound card, type "help sound" (without the double quot

figure

plot((0:1/(f0*100):total_time), x_CT,'-', 'linewidth', 2); % CT signal


hold on
stem(t, x_DT,'r', 'linewidth', 2); % DT signal
plot(t, x_DT,'r', 'linewidth', 2); % connet the dots, i.e., connect DT x[n]

3
xlabel('Time (sec.)');
ylabel('x(nT)');
title('Discrete time sinusoid (time domain)');
axis([0 1/f0*5 -A A]); % only observe the signal from time 0 to time 1/f0*3. once you remove t
legend('x(t)', 'x[n]', 'connected x[n]')

1.a:如果振幅A的值越大,聽到的聲音越大

f0 = 2000; % in Hz
total_time = 5; % in sec.

% !!! Sampling in time


fsRatio = 20;
fs = f0*fsRatio;
T = 1/fs; % sampling interval in time
A = 1; % magnitude
phi = 0; % phase

t = (0:T:total_time); % time axis


% x(t) = A*cos(2*pi*f0*t+phi) where A: magnitude (>= 0), f0: 1/(fundamental period), phi: phase
x_CT = A*cos(2*pi*f0*(0:1/(f0*100):total_time)+phi); % x(t), sampling rate is high enough so th
x_DT = A*cos(2*pi*f0*t+phi); % x[n] = x(nT), sampled cosine/discrete time sinusoid
%Npoint = length(x_DT); % number of points in sampled cosine
sound(x_DT,fs); % play the signal by the sound card, type "help sound" (without the double quot

figure

4
% type "help plot" (without the double quote) under MATLAB console to see the usage of plot().
% type "help stem" to see the usage of stem().
%plot((0:1/(f0*100):total_time), x_CT,'-o', 'linewidth', 2);
plot((0:1/(f0*100):total_time), x_CT,'-', 'linewidth', 2); % CT signal
hold on
stem(t, x_DT,'r', 'linewidth', 2); % DT signal
plot(t, x_DT,'r', 'linewidth', 2); % connet the dots, i.e., connect DT x[n]
xlabel('Time (sec.)');
ylabel('x(nT)');
title('Discrete time sinusoid (time domain)');
axis([0 1/f0*5 -A A]); % only observe the signal from time 0 to time 1/f0*3. once you remove t
legend('x(t)', 'x[n]', 'connected x[n]')

f0 = 4000; % in Hz
total_time = 5; % in sec.

% !!! Sampling in time


fsRatio = 20;
fs = f0*fsRatio;
T = 1/fs; % sampling interval in time
A = 1; % magnitude
phi = 0; % phase

t = (0:T:total_time); % time axis


% x(t) = A*cos(2*pi*f0*t+phi) where A: magnitude (>= 0), f0: 1/(fundamental period), phi: phase
x_CT = A*cos(2*pi*f0*(0:1/(f0*100):total_time)+phi); % x(t), sampling rate is high enough so th

5
x_DT = A*cos(2*pi*f0*t+phi); % x[n] = x(nT), sampled cosine/discrete time sinusoid
%Npoint = length(x_DT); % number of points in sampled cosine
sound(x_DT,fs); % play the signal by the sound card, type "help sound" (without the double quot

figure
% type "help plot" (without the double quote) under MATLAB console to see the usage of plot().
% type "help stem" to see the usage of stem().
%plot((0:1/(f0*100):total_time), x_CT,'-o', 'linewidth', 2);
plot((0:1/(f0*100):total_time), x_CT,'-', 'linewidth', 2); % CT signal
hold on
stem(t, x_DT,'r', 'linewidth', 2); % DT signal
plot(t, x_DT,'r', 'linewidth', 2); % connet the dots, i.e., connect DT x[n]
xlabel('Time (sec.)');
ylabel('x(nT)');
title('Discrete time sinusoid (time domain)');
axis([0 1/f0*5 -A A]); % only observe the signal from time 0 to time 1/f0*3. once you remove t
legend('x(t)', 'x[n]', 'connected x[n]')

1.b:f0從1000開始調高到4000,音調變得越來越高

f0 = 1000; % in Hz
total_time = 5; % in sec.

% !!! Sampling in time


fsRatio = 20;

6
fs = f0*fsRatio;
T = 1/fs; % sampling interval in time
A = 1; % magnitude
phi = pi/4; % phase

t = (0:T:total_time); % time axis


% x(t) = A*cos(2*pi*f0*t+phi) where A: magnitude (>= 0), f0: 1/(fundamental period), phi: phase
x_CT = A*cos(2*pi*f0*(0:1/(f0*100):total_time)+phi); % x(t), sampling rate is high enough so th
x_DT = A*cos(2*pi*f0*t+phi); % x[n] = x(nT), sampled cosine/discrete time sinusoid
%Npoint = length(x_DT); % number of points in sampled cosine
sound(x_DT,fs); % play the signal by the sound card, type "help sound" (without the double quot

figure
% type "help plot" (without the double quote) under MATLAB console to see the usage of plot().
% type "help stem" to see the usage of stem().
%plot((0:1/(f0*100):total_time), x_CT,'-o', 'linewidth', 2);
plot((0:1/(f0*100):total_time), x_CT,'-', 'linewidth', 2); % CT signal
hold on
stem(t, x_DT,'r', 'linewidth', 2); % DT signal
plot(t, x_DT,'r', 'linewidth', 2); % connet the dots, i.e., connect DT x[n]
xlabel('Time (sec.)');
ylabel('x(nT)');
title('Discrete time sinusoid (time domain)');
axis([0 1/f0*5 -A A]); % only observe the signal from time 0 to time 1/f0*3. once you remove t
legend('x(t)', 'x[n]', 'connected x[n]')

1.c:改變相位不會發生音調音量上的變化,改變相位角是time shift

7
8

You might also like