# fourier transform

Discussion in 'Mathematics and Physics' started by PG1995, Nov 8, 2017.

1. ### PG1995Active Member

Joined:
Apr 18, 2011
Messages:
1,672
Likes:
13
Hi,

Could you please help me to fix the code below? I know that the code is completely wrong but I have tried to write it down so that you can get an idea what I'm trying to do.

I'm trying to understand the fourier transform and I needed the code to formulate my questions. Thank you.

Code (text):

%%file name fourier.m
clear all; close all; clc;
f = 0; %% 0 Hz to 100 Hz, with 0.1 step, there would be 1000 frequencies
cos_v=[1:1:1000]; %%vector to store values
f_w=[]; %% defining a vector

for i1=[0:1000]; %% for loops begins, with loop index i1
f_w = sqrt(1/(4*f));
cos_v(i1)=f_w*cos(2*pi*f*x); %% i1 serves as an index for cos_v vector elements too
f=f+0.1;
end

combined_sinusoids=sum(cos_v); %% cos_v(1)+cos_v(2)+cos_v(3)+...
x = [0:0.2:500];
y = combined_sinusoids;
plot(x, y)

Last edited: Nov 8, 2017
2. ### RatchitWell-Known Member

Joined:
Mar 12, 2008
Messages:
1,948
Likes:
83
At least you could disclose what language the code is written. Do you expect us to get up to speed on that particular language and debug your code? Perhaps a more direct question would be appropriate.

Ratch

3. ### steveBWell-Known MemberMost Helpful Member

Joined:
Jan 16, 2009
Messages:
1,300
Likes:
631
I see this is coded in Matlab, and there will be many people here that are familiar with Matlab coding, but I have to echo the basic sentiment that you haven't provided us a good effort in asking this question. When I run this code, I get an error on line 9 that says x is not defined. I see it is defined later in the code, but why not before? Basically, it is not clear at all what you are trying to do. It would be better for you to provide the equations you wrote out before you started coding. ... Hmm, did you do that? If not, that's a big mistake. Your method should be well planned out before you even begin coding.

That said, what definition of "Fourier Transform" are you using? It looks like you are only considering the cosine component and not the sine component. The usual Fourier transform is going to involve complex values from the i*sin piece, or the exp (i x) if you do it from that definition.

To reiterate, provide the definition of the Fourier transform (continuous or discrete, exp or sin/cos etc.), then a derivation of how you get to the form you will implement in code (i.e integrals converted into summations , variable definitions, index definitions etc.). And, most importantly, the code you do provide should run without errors.

Last edited: Nov 9, 2017
• Like x 1

Joined:
Jan 12, 1997
Messages:
-
Likes:
0

5. ### PG1995Active Member

Joined:
Apr 18, 2011
Messages:
1,672
Likes:
13

Thank you, steveB.

Ratchit: It's Matlab code. Sorry, I should have mentioned it.

You could check this PDF to see what I'm trying to do. I have been trying to get a conceptual understanding of Fouier transform like how it basically works and why it works as a kind of series representation of a function.

How could I transform FORM #2 into FORM #1? Thank you.

Re: Code
I believe MATLAB treats every variable (even a constant) as a vector. A vector is a one-dimensional array and a matrix is two-dimensional array. I've experience with C++ so I'd tend to stick twith he syntax of C++ so as I learn more about MATLAB I'll start thinking more in MATLAB terms.

Given below is the code with some revisions; also the error.

Code (Matlab):

%%file name fourier.m
clear all; close all; clc;
f = [0.1]; %%defining and initializing a constant vector
%%0 Hz to 100 Hz, with 0.1 step, there would be 1000 frequencies
cos_v=zeros(1000); %%defining and initializing vector to store values
%%a vector with 1000 zeroes
f_w=[0]; %% defining and initializing a vector, fourier transfor,
syms x; %%"x" is a floating point variable which can take values from 0 to
%%infinity, in C++ we could have written "float x"

for i1=[1:1000] %% for loops begins, with loop index i1
f_w = sqrt(1/(4*f)); %% fourier transform of 1/sqrt(x)
cos_v(i1)=f_w.*cos(2*pi*f*x); %% i1 serves as an index for cos_v vector elements too
f=f+0.1; %%incrementing f
f_w=0; %%setting f_w=0 for next iteration
end

combined_sinusoids=sum(cos_v); %% cos_v(1)+cos_v(2)+cos_v(3)+...
y = combined_sinusoids;

x = input('Enter value of x: ');

fprintf('The value of combined_sinusoids is %d \n',y)

fprintf('The value of 1/sqrt(x) is %d \n',1/sqrt(x))

fprintf('Difference between 1/sqrt(x) and approximation by combined_sinusoids is %d \n',abs(y-(1/sqrt(s))))

Code (Matlab):

??? The following error occurred converting from sym to double:
Error in MuPAD command: DOUBLE cannot convert the input expression into a double array.

If the input expression contains a symbolic variable, use the VPA function instead.

Error in ==> fourier_new at 13
cos_v(i1)=f_w*cos(2*pi*f*x); %% i1 serves as an index for cos_v vector elements too

Last edited: Nov 12, 2017 at 3:51 PM
6. ### steveBWell-Known MemberMost Helpful Member

Joined:
Jan 16, 2009
Messages:
1,300
Likes:
631
It's still not 100 percent clear what you are trying to do, but I will take a guess and you can tell me if I'm correct. Before that, I will mention that your code is trying to use symbolic math commands. I don't have the symbolic toolbox installed, so I can't run the code, but I wonder why you are using symbolic processing. If the symbolic processor can do it, then you can do it too, by hand. Usually Fourier transform programs are intended to be numerical to handle general cases that can't be solved symbolically. Also, you are using a Cosine Fourier Transform and not the usual Fourier Transform. I'm not sure why you are using that one, but it is fine because it is just an integral that is define for functions that allow it to converge.

OK, so I think what you are trying to do is simply calculate a numerical estimation of a one dimensional integral. This should be straightforward if you convert the integral to an approximate summation using any of the standard numerical integral techniques. Have you ever used the "Trapazoidal Rule" or "Simpson's Rule"? I would recommend this approach, and start with the trapazoidal rule, which is simpler. Then just code it in a loop. No need for anything complicated.

http://mathworld.wolfram.com/SimpsonsRule.html
https://en.wikipedia.org/wiki/Simpson's_rule
https://en.wikipedia.org/wiki/Trapezoidal_rule

• Like x 1