Why does ais not the same as b in matlab??

a = signal;
b = exp(log(signal));

if I plot a and b, the signal is not the same, any help?

有帮助吗?

解决方案

The logarithm of a negative number, -x, is y = log(x)+pi*1i. Thus when you apply the exponential function to y you will be left with a zero imaginary part (or something that looks like zero). Try this for example:

format long
x = -1;
y = exp(log(x))
abserr = abs(x-y)

Read more about the complex logarithm here.

There can also be inaccuracy due to floating point of course. The absolute error can be especially significant if your signal has values anywhere near to 1/eps (or -1/eps). Try

x = 1/eps;
y = exp(log(x));
abserr = abs(y-x)  
relerr = abs(y-x)/abs(x)

which returns

abserr =

  11.500000000000000


relerr =

     2.553512956637860e-15

Note that the relative error is tiny. In floating point calculations relative error is generally what we wish to control.

其他提示

horchler's answer is of course right – but I think it misses the plotting part which causes the OP's confusion. Since Matlab uses complex numbers when necessary, log is defined also for negative numbers, and exp should be the exact inverse of log – except for rounding errors. The numerical difference between signal and exp(log(signal)) should almost always be very small. An example:

>> signal = (-2:2)'

signal =

    -2
    -1
     0
     1
     2

>> exp(log(signal))

ans =

                         -2 +  2.44929359829471e-16i
                         -1 +  1.22464679914735e-16i
                          0 +                     0i
                          1 +                     0i
                          2 +                     0i

However, the behavior of plot differs depending on whether the argument is a real or a complex number:

subplot(1, 2, 1)
plot(signal, '.-')
subplot(1, 2, 2)
plot(exp(log(signal)), '.-')

results in

Though the two vectors have a negligible numerical difference, the plots look completely different.

That is because if called with a single real-valued vector as an argument, plot uses the given vector values for the vertical axis and the vector indices (1, 2, …) for the horizontal axis. However, if called with a single complex-valued vector, plot uses the real part for the horizontal axis and the imaginary part for the vertical axis. I.e., the two calls to plot in the above code are equivalent to

plot(1 : 5, signal, '.-')

and

plot(real(exp(log(signal))), imag(exp(log(signal))), '.-')

respectively.

If you use log with complex or negative values, Matlab calculates the complex logarithm. This will yield unexpected results.

a = abs(signal);
b = exp(log(a));

will work as you expect it to (although the signal's complex part and sign will be lost in the process).

b = log(exp(signal));

will also work because it doesn't need to handle complex logarithms.

In other words, you have to make sure to respect your functions' domains. The real (as in non-complex) log's domain is all positive(!) real numbers while real exp's domain is all numbers. On the other hand, log's image is all real numbers while real exp's is all positive real numbers. That's why it works one way, but not the other. You're trying to do something the real log can't do, so Matlab switches to the complex log. While exp still reverses it, you will get complex results which are handled differently and can look entirely different depending on what you do with them afterwards.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top