How to implement Complex Exponential Function Using Matlab ?

Please
How can I implement the Complex Exponential Function mentioned in this equation:
X(N) =exp^(j*w1*N) + exp^(j*w2*N)
Suppose:
F =40KHZ
N=Range 1 to 20
w1: is the first angular frequency
w1=2*pi*F
w2: is the second angular frequency
w2=2*pi*F
Finally,
display the values are saved in X(N) in command window.
We apply fast fourier transform (FFT) on X(N) Then we plot the result.

 Respuesta aceptada

I am not certain what you are doing or what ‘w1’ and ‘w2’ are, so using my best guess —
format longE
F = 40E+3;
N = (0:20).';
omega1 = 2*pi*F; % Guessing: 'W1'
omega2 = 4*pi*F; % Guessing: 'w2'
X = exp(1j*omega1*N) + exp(1j*omega2*N)
X =
2.000000000000000e+00 + 0.000000000000000e+00i 2.000000000000000e+00 - 1.165637649496438e-11i 2.000000000000000e+00 - 2.331275298992875e-11i 2.000000000000000e+00 - 3.496912948489312e-11i 2.000000000000000e+00 - 4.662550597985751e-11i 2.000000000000000e+00 + 2.909640830059826e-10i 2.000000000000000e+00 - 6.993825896978625e-11i 2.000000000000000e+00 - 4.308406009455551e-10i 2.000000000000000e+00 - 9.325101195971502e-11i 2.000000000000000e+00 + 2.443385770261251e-10i 2.000000000000000e+00 + 5.819281660119652e-10i 2.000000000000000e+00 - 4.774661069254125e-10i 2.000000000000000e+00 - 1.398765179395725e-10i 2.000000000000000e+00 + 1.977130710462676e-10i 2.000000000000000e+00 - 8.616812018911102e-10i 2.000000000000000e+00 - 5.240916129052701e-10i 2.000000000000000e+00 - 1.865020239194300e-10i 2.000000000000000e+00 - 1.245896296856808e-09i 2.000000000000000e+00 + 4.886771540522501e-10i 2.000000000000000e+00 - 5.707171188851276e-10i 2.000000000000000e+00 + 1.163856332023930e-09i
.

13 comentarios

Muhammad Salem
Muhammad Salem el 3 de Nov. de 2023
Editada: Muhammad Salem el 3 de Nov. de 2023
I'm sorry, I updated the question, please reconsider it :
w1: is the first angular frequency
w1=2*pi*F
w2: is the second angular frequency
w2=2*pi*F
I'm sure you made the obvious change:
omega2 = 2*pi*F;
so what went wrong? Why are you still asking? I can only assume it's not working for you. Be explicit about why Star's code with your new change of omega2 value is not working.
Muhammad Salem
Muhammad Salem el 3 de Nov. de 2023
Editada: Muhammad Salem el 3 de Nov. de 2023
Yes, I am asking because I have a problem with the results and I do not know what the solution is.
Here are the results:
2.0000 + 0.0000i
2.0000 - 0.0000i
2.0000 - 0.0000i
2.0000 - 0.0000i
2.0000 - 0.0000i
2.0000 + 0.0000i
2.0000 - 0.0000i
2.0000 - 0.0000i
2.0000 - 0.0000i
2.0000 + 0.0000i
2.0000 + 0.0000i
2.0000 - 0.0000i
2.0000 - 0.0000i
2.0000 + 0.0000i
2.0000 - 0.0000i
2.0000 - 0.0000i
2.0000 - 0.0000i
2.0000 - 0.0000i
2.0000 + 0.0000i
2.0000 - 0.0000i
2.0000 + 0.0000i
O.K., so ‘w1’ and ‘w2’ are actually the same thing.
My code then becomes:
format longE
F = 40E+3;
N = (0:20).';
omega1 = 2*pi*F; % Guessing: 'W1'
omega2 = 2*pi*F; % Guessing: 'w2'
X = exp(1j*omega1*N) + exp(1j*omega2*N)
X =
2.000000000000000e+00 + 0.000000000000000e+00i 2.000000000000000e+00 - 7.770917663309584e-12i 2.000000000000000e+00 - 1.554183532661917e-11i 2.000000000000000e+00 - 2.331275298992875e-11i 2.000000000000000e+00 - 3.108367065323834e-11i 2.000000000000000e+00 + 1.939760553373217e-10i 2.000000000000000e+00 - 4.662550597985750e-11i 2.000000000000000e+00 - 2.872270672970367e-10i 2.000000000000000e+00 - 6.216734130647667e-11i 2.000000000000000e+00 + 1.628923846840834e-10i 2.000000000000000e+00 + 3.879521106746434e-10i 2.000000000000000e+00 - 3.183107379502750e-10i 2.000000000000000e+00 - 9.325101195971500e-11i 2.000000000000000e+00 + 1.318087140308451e-10i 2.000000000000000e+00 - 5.744541345940735e-10i 2.000000000000000e+00 - 3.493944086035134e-10i 2.000000000000000e+00 - 1.243346826129533e-10i 2.000000000000000e+00 - 8.305975312378718e-10i 2.000000000000000e+00 + 3.257847693681668e-10i 2.000000000000000e+00 - 3.804780792567517e-10i 2.000000000000000e+00 + 7.759042213492868e-10i
format shortE
X
X =
2.0000e+00 + 0.0000e+00i 2.0000e+00 - 7.7709e-12i 2.0000e+00 - 1.5542e-11i 2.0000e+00 - 2.3313e-11i 2.0000e+00 - 3.1084e-11i 2.0000e+00 + 1.9398e-10i 2.0000e+00 - 4.6626e-11i 2.0000e+00 - 2.8723e-10i 2.0000e+00 - 6.2167e-11i 2.0000e+00 + 1.6289e-10i 2.0000e+00 + 3.8795e-10i 2.0000e+00 - 3.1831e-10i 2.0000e+00 - 9.3251e-11i 2.0000e+00 + 1.3181e-10i 2.0000e+00 - 5.7445e-10i 2.0000e+00 - 3.4939e-10i 2.0000e+00 - 1.2433e-10i 2.0000e+00 - 8.3060e-10i 2.0000e+00 + 3.2578e-10i 2.0000e+00 - 3.8048e-10i 2.0000e+00 + 7.7590e-10i
whos('X')
Name Size Bytes Class Attributes X 21x1 336 double complex
The imaginary parts are very close to zero, so using a more restricted format option, they will appear as zero. Using extended precision, this is readily apparent, and with greater precision or exponential notation (or both) this becomes clear.
EDIT — (3 Nov 2023 at 20:43)
My code does exactly what you asked, both in the original and in the edit. The X result is a (21x1) column vector, showing both the real and imaginary parts. The size is (21x1) because N includes zero, for a total of 21 elements. If you want it to be exactly (20x1) use the linspace function —
format longE
F = 40E+3;
N = linspace(0, 20, 20).'
N = 20×1
0 1.052631578947368e+00 2.105263157894737e+00 3.157894736842105e+00 4.210526315789473e+00 5.263157894736842e+00 6.315789473684211e+00 7.368421052631579e+00 8.421052631578947e+00 9.473684210526315e+00
omega1 = 2*pi*F;
omega2 = 2*pi*F;
X = exp(1j*omega1*N) + exp(1j*omega2*N)
X =
2.000000000000000e+00 + 0.000000000000000e+00i -1.651586909120879e-01 + 1.993168986016039e+00i -1.972722606816205e+00 - 3.291891804969826e-01i 4.909709741865338e-01 - 1.938800531902734e+00i 1.891634483443725e+00 + 6.493989382856979e-01i -8.033908495827032e-01 + 1.831546653188715e+00i -1.758947502506326e+00 - 9.518947859016555e-01i 1.093896316053294e+00 - 1.674332956650209e+00i 1.578281018953408e+00 + 1.228425425172970e+00i -1.354563142692437e+00 + 1.471447821860900e+00i -1.354563142806782e+00 - 1.471447821755638e+00i 1.578281018857948e+00 - 1.228425425295617e+00i 1.093896316573241e+00 + 1.674332956310511e+00i -1.758947502432355e+00 + 9.518947860383419e-01i -8.033908497250313e-01 - 1.831546653126284e+00i 1.891634483393261e+00 - 6.493989384326954e-01i 4.909709747886086e-01 + 1.938800531750268e+00i -1.972722606790624e+00 + 3.291891806502813e-01i -1.651586924591879e-01 - 1.993168985887843e+00i 2.000000000000000e+00 + 7.759042213492868e-10i
format short
X
X =
2.0000 + 0.0000i -0.1652 + 1.9932i -1.9727 - 0.3292i 0.4910 - 1.9388i 1.8916 + 0.6494i -0.8034 + 1.8315i -1.7589 - 0.9519i 1.0939 - 1.6743i 1.5783 + 1.2284i -1.3546 + 1.4714i -1.3546 - 1.4714i 1.5783 - 1.2284i 1.0939 + 1.6743i -1.7589 + 0.9519i -0.8034 - 1.8315i 1.8916 - 0.6494i 0.4910 + 1.9388i -1.9727 + 0.3292i -0.1652 - 1.9932i 2.0000 + 0.0000i
.
Can you do FOR LOOP instead of using linspace ?
The linspace function just creates the vector with 20 evenly-spaced elements between 0 and 20 (a opposed to the colon operator that produces 21 elements). A for loop would not change that from the vectorised version. It would simply be a bit less efficient.
It is possible to do that, however a for loop (without using linspace to create the ‘N’ vector) would produce the same as the colon operator. It would not replace the linspace call.
Thanks for your answer...
But I am required to implement it using For Loop. Can you implement it and tell me the results?
Nvalues = linspace(0, 20, 20).';
num_N = length(Nvalues);
X = zeros(num_N, 1);
for Nidx = 1 : num_N
N = Nvalues(Nidx);
now do your calculation for this N
X(Nidx) = result for this N;
end
Why did you use the linspace command?
I don't need it because the for loop (Nidx) will be in place N.
Your requirements from the initial question were:
N=Range 0 to 20
Finally, the values are saved in a vector 20 * 1
So you need the initial value to be 0, the final value to be 20, and for there to be 20 values total.
In order to do that as a for loop without using the linspace structure I show here, you would need
for N = 0:20/19:20
which will get you N values such as 0 1.05263157894737 2.10526315789474 but the 20'th will be 20.
Now that you are looping incrementing by 20/19 at a time... how will you know what index to store the current result at?
@Muhammad Salem — As always, my pleasure!
@Walter Roberson — Thank you!
I am really sorry
The question has been updated. Perhaps the question contains some errors or may not be clear.
Please give me the answer again
That simply requires minor tweaks to the existing code.
See the documentation on the fft function to perform the Fourier transform. There are abundant examples throughout Answers. (I wrote many of them.)
That result may not be very exciting to view, since ‘X’ is created by returning integer multiples of .

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 3 de Nov. de 2023

Comentada:

el 4 de Nov. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by