Summing two signals should result in zero, but doesn't

5 次查看(过去 30 天)
Hello,
I'm new to MATLAB, doing an introductory course on systems & signals and am trying to code a simple program to define two signals and sum them.
Summing a signal with itself results in the correct output: the original signal with double the amplitude.
Summing two signals that are only separated by a phase of pi should result in zero, right? But that's not what happens here. What am I doing wrong?
% define signal 1
n_max = 5;
A = 1;
f = 2;
theta = 0;
offset = 0;
t = -n_max:0.01:n_max;
sinusoid = A*cos(2*pi*f*t + theta) + offset;
% define signal 2
n_max_2 = 5;
A_2 = 1;
f_2 = 2;
theta_2 = pi;
offset_2 = 0;
sinusoid_2 = A_2*cos(2*pi*f_2*t + theta_2) + offset_2;
% sum and plot the resulting signal
sum_signal = sinusoid + sinusoid_2;
plot(t, sum_signal);
xlabel('time');
ylabel('signal');
xlim([-n_max n_max]);
grid;
title('Sum signal');
  1 个评论
Dyuman Joshi
Dyuman Joshi 2024-1-28
It is because the cos() function is not accurate enough for some inputs.
A more accurate function is cospi, which you can utilize here -
% define signal 1
n_max = 5;
A = 1;
f = 2;
theta = 0;
offset = 0;
t = -n_max:0.01:n_max;
sinusoid = A*cospi(2*f*t + theta/pi) + offset;
% define signal 2
n_max_2 = 5;
A_2 = 1;
f_2 = 2;
theta_2 = pi;
offset_2 = 0;
sinusoid_2 = A_2*cospi(2*f_2*t + theta_2/pi) + offset_2;
% sum and plot the resulting signal
sum_signal = sinusoid + sinusoid_2;
plot(t, sum_signal);
xlabel('time');
ylabel('signal');
xlim([-n_max n_max]);
grid;
title('Sum signal');
As you can see, apart from a few disturbances, the values are 0.
Numerically computing trignometric functions has limitations. Adding the limitation of double precision values, values will be a bit off (as observed above), but even then the margin is miniscule (~1e-15)

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2024-1-28
Round-off error. The calculation of cos() is not perfect.
You can reduce the round-off error by using cospi()
% define signal 1
n_max = 5;
A = 1;
f = 2;
theta = 0/pi;
offset = 0;
t = -n_max:0.01:n_max;
sinusoid = A*cospi(2*f*t + theta) + offset;
% define signal 2
n_max_2 = 5;
A_2 = 1;
f_2 = 2;
theta_2 = pi/pi;
offset_2 = 0;
sinusoid_2 = A_2*cospi(2*f_2*t + theta_2) + offset_2;
% sum and plot the resulting signal
sum_signal = sinusoid + sinusoid_2;
plot(t, sum_signal);
xlabel('time');
ylabel('signal');
xlim([-n_max n_max]);
grid;
title('Sum signal');

更多回答(1 个)

Sulaymon Eshkabilov
Note that the matlab's PRECISION is not ABSOLUTE. See this example of Pythogorian theorem:
a = -pi:pi/100:pi;
F = 1 - (sin(a).^2+cos(a).^2);
Index = find(F==0);
F0= F(Index);
plot(a, F, 'k-', a(Index), F0, 'r*'); % Check also Y axis scale
grid on
legend('All F values', 'F = 0')
xlabel('\alpha')
ylabel('$F = 1 - (sin^2(\alpha)+cos^2(\alpha))$', 'Interpreter', 'Latex')
fprintf('Number of Exact F = 0 is %d of out of total calculated values of F %d \n', numel(F0), numel(F))
Number of Exact F = 0 is 153 of out of total calculated values of F 201

类别

Help CenterFile Exchange 中查找有关 Localization 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by