Matlab syntax help using fourier series & vectors

In summary, the conversation is about understanding MATLAB code for the Fourier series approximation of a signal. The code involves a loop and an element-wise operator, which can be used to compute the coefficients and create a new vector with the exponents of the original vector. Overall, the conversation is focused on comprehending the syntax and how the code works.
  • #1
radiodude
14
0
I'm trying to understand some MATLAB code. It's the Fourier series approximation of a signal. This is my first time using matlab, so I guess I'm really trying to comprehend the syntax.

For my example, I am using some code I googled (http://www.ee.nmt.edu/~wedeward/EE341/FA97/example8.html)

Code:
01 N = 11;                            % summation limit (use N odd)
02 wo = pi;                           % fundamental frequency (rad/s)
03 c0 = 0;                            % dc bias
04 t = -3:0.01:3;                     % declare time values
05 
06 yce = c0*ones(size(t));            % initialize yce to c0
07 
08 for n = -N:2:N,                    % loop over series index n
09   cn = 2/(j*n*wo);                 % Fourier Series Coefficient
10   yce = yce + cn*exp(j*n*wo*t);    % Fourier Series computation
11 end

What I don't understand is the yce = yce + ... line (line 10). On that line you have
yce [vector]
cn [scalar]
j [scalar]
n [scalar]
wo [scalar]
t [vector]

So what does
vector = vector + scalar * exp(scalar * scalar * scalar * vector)
really evaluate to in matlab?

Does exp(vector) really mean it creates a new vector with entries:
e^old_vec_entry_for_t0
e^old_vec_entry_for_t1
...
e^old_vec_entry_for_tn

If so, then it makes sense as then it resolves to
vector = vector + scalar*vector

which is then

vector_running_sum = vector_running_sum + vector_values_for_all_t_from_neg_n_to_pos_n
 
Last edited:
Physics news on Phys.org
  • #2
Your interpretation is correct. When you do vectorB=exp(vectorA), it produces a vector of the same size as vectorA containing the exponents of each of the values in vectorA.

This is an element-wise operator. Sometimes, you need to specify (usually by preceding the operator with a period, for instance ./ is element-wise division). For more, see:
http://www.cyclismo.org/tutorial/matlab/operations.html
 
  • #3
I would first commend you for seeking help and trying to understand the code. MATLAB can be a powerful tool for scientific research, but it can also be overwhelming for beginners. Let me try to break down the syntax and provide some explanation to help you understand the code better.

First, let's start with the overall purpose of the code. It is trying to approximate a signal using Fourier series. This involves breaking down a signal into a sum of sine and cosine functions with different frequencies and amplitudes. The code is using a loop to calculate the coefficients (cn) for each term in the series and then adding them together to get the final approximation (yce).

Now, let's look at line 10, which is causing confusion for you. This line is essentially saying "update the yce vector by adding the current term in the series to it." Let's break down the components of this line:

- yce [vector]: This is the vector that represents the current approximation of the signal.
- cn [scalar]: This is the coefficient for the current term in the series.
- j [scalar]: This is the imaginary unit in MATLAB.
- n [scalar]: This is the index for the current term in the series.
- wo [scalar]: This is the fundamental frequency of the signal.
- t [vector]: This is the vector of time values.

Now, let's look at the right side of the equation. The exp() function in MATLAB calculates the exponential of a given value. In this case, it is being used to calculate the value of the current term in the series, which is a complex exponential function. This function takes in the imaginary unit (j), the index (n), the fundamental frequency (wo), and the time values (t) to calculate the value of the current term for each time point.

To answer your question, yes, exp(vector) does create a new vector with exponential values of the entries in the original vector. It is essentially performing the operation you mentioned - e^old_vec_entry_for_t0, e^old_vec_entry_for_t1, etc.

So, the line 10 is essentially updating the yce vector by adding the current term in the series (cn*exp(j*n*wo*t)) to it. This is repeated for each term in the series, resulting in the final approximation of the signal.

I hope this explanation helps you understand the code better. Keep practicing and exploring,
 
  • #4
Hello,

I understand your confusion with the syntax of the MATLAB code you are trying to understand. Let me break it down for you:

Line 1: N is a variable that represents the summation limit. In this case, it is set to 11.
Line 2: wo is a variable that represents the fundamental frequency, which is set to pi (rad/s).
Line 3: c0 is a variable that represents the dc bias, which is set to 0.
Line 4: t is a vector that represents the time values, which are set from -3 to 3 with a step size of 0.01.
Line 6: yce is a vector that is initialized to have the same size as t and is filled with c0 values.
Line 8: This is a for loop that iterates over the series index n, starting from -N, increasing by 2, and ending at N.
Line 9: cn is a scalar variable that represents the Fourier Series Coefficient, which is calculated using the formula 2/(j*n*wo).
Line 10: This line is where the Fourier Series computation happens. Here, yce is updated by adding the product of cn and the exponential function of j*n*wo*t. This is how the Fourier series is approximated mathematically.
Line 11: The for loop ends here.

To answer your question, yes, exp(vector) creates a new vector with entries that are the exponential function of the original vector. In this case, it is used to calculate the Fourier series approximation for each time value in the vector t.

I hope this helps you understand the syntax and purpose of the code. MATLAB can be a powerful tool for scientific computations, and with practice, you will become more comfortable with its syntax and functions. Keep exploring and learning!
 

Related to Matlab syntax help using fourier series & vectors

1. What is a Fourier series in Matlab?

A Fourier series in Matlab is a mathematical representation of a periodic function as a sum of sine and cosine functions. It is used to analyze and approximate complex functions and signals.

2. How do I input a function into a Fourier series in Matlab?

To input a function into a Fourier series in Matlab, you can use the "syms" function to define symbolic variables and then use the "fourier" function to compute the Fourier series coefficients.

3. Can I use vectors in a Fourier series in Matlab?

Yes, you can use vectors in a Fourier series in Matlab. Vectors can be used to represent the coefficients of the sine and cosine functions in the Fourier series.

4. How do I plot a Fourier series in Matlab?

To plot a Fourier series in Matlab, you can use the "plot" function to plot the individual sine and cosine functions, and then add them together to see the resulting Fourier series.

5. Are there any built-in functions for Fourier series in Matlab?

Yes, there are several built-in functions for Fourier series in Matlab, such as "fourier" for computing the coefficients, "ifft" for inverse Fourier transform, and "fft" for fast Fourier transform.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
26
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
998
  • Calculus and Beyond Homework Help
Replies
2
Views
514
Replies
4
Views
663
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
4K
Back
Top