Summing two signals should result in zero, but doesn't
조회 수: 3 (최근 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
2024년 1월 28일
It is because the cos() function is not accurate enough for some inputs.
% 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
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');
댓글 수: 0
추가 답변 (1개)
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))
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Localization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!