How to replace complex numbers in a column vector with 0.000+0.000i ?

34 views (last 30 days)
FW on 17 Oct 2019
Answered: Walter Roberson on 17 Oct 2019
Image we obtain a fft of a signal and we wish to remove certain frequencies from the signal. I want to fill in zeros after removing certain rows of complex numbers. Filling in with zeros [0, 0, 0, 0] did not work because ifft was absurd. The real part and the imaginary part must be zeroed and it should appear like 0.0000+0.0000i. Is there a way to fill in 0000+0.0000i in desired regions of the FFT of the signal? What command should one use? Basically this is fft filtering.
Say fft(A) = [-0.0708535039155114 - 0.00736177322883826i, -0.0700055929768761 - 0.0329196686985671i, -0.0593736872430442 + 0.00109491019902367i,
-0.0435002106533236 + 0.0325121703659659i, -0.0605346303385998 - 0.000499071340404484i]
How should we convert that into by removing certain complex numbers and convert them into zero complex numbers?
[-0.0708535039155114 - 0.00736177322883826i, 0.000000 +0.0000000000i, 0.000000 +0.0000000000i, 0.000000 +0.0000000000i,- 0.000499071340404484i]

Accepted Answer

Walter Roberson
Walter Roberson on 17 Oct 2019
Supposing the values are stored in fft_A then
fft_A(2:4) = 0;
is enough. When you display fft_A afterwards you will see the complex zeros are there. The complex 0 component will not show up if you extract the particular value, such as fft_A(2) but it is still stored: you would just be seeing the effect that fft_A(2) would be an expression that calculates a value in some way, and MATLAB automatically checks whether all imaginary components of an expression are 0 and if so drops the complex part.
There are not many circumstances under which you need an array that has all-zero complex part to be explicitly marked as complex. It is possible though:
X = complex(real(X), imag(X));
will have the side effect of marking X as complex even if all the complex components are 0. But as soon as you store anything into X then the complex part would be dropped, and the complex part would be dropped on anything extracted from X.

More Answers (1)

Jon on 17 Oct 2019
Edited: Jon on 17 Oct 2019
Suppose you have a complex vector (the result of your fft) lets call it x. If you want to replace some elements of it with zero (0 + 0i), you can just use for example to replace the 3rd and 4th and 6th element with zero
x = randn(10,1) + randn(10,1)*i
x([3 4 6])=0
which gives
0.2939 + 1.3703i
-0.7873 - 1.7115i
0.0000 + 0.0000i
0.0000 + 0.0000i
-1.0689 + 0.3192i
0.0000 + 0.0000i
-2.9443 - 0.8649i
1.4384 - 0.0301i
0.3252 - 0.1649i
-0.7549 + 0.6277i
Note that if the rest of the vector is complex, you can just assign the value 0 (no complex part) to the elements and it will make both the real and imaginary parts zero




Community Treasure Hunt

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

Start Hunting!

Translated by