Summing two signals should result in zero, but doesn't
Afficher commentaires plus anciens
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 commentaire
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)
Réponse acceptée
Plus de réponses (1)
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))
Catégories
En savoir plus sur Descriptive Statistics dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



