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

조회 수: 9 (최근 30일)
Miro Kakkonen
Miro Kakkonen 2024년 1월 28일
댓글: Dyuman Joshi 2024년 1월 28일
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
Sulaymon Eshkabilov 2024년 1월 28일
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에서 Time Series Events에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by