%%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)
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.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:%%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)
%%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))))
??? The following error occurred converting from sym to double:
Error using ==> mupadmex
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
Something seems wrong here. What is the variable "n" representing.
Also, the integral should be over the dx, not dw.
That's pi.
You're right. It should have been dx. The mentioned source document had it wrong and I didn't notice it.
Cosine Fourier Transform:
[latex]F(\omega )=\frac{2}{\pi }\int_{0}^{\infty }f(x)\cos (\omega x)dx[/latex]
Inverse Cosine Fourier Transform:
[latex]f(x)=\int_{0}^{\infty }F(\omega )\cos (\omega x)d\omega[/latex]
So, again, the cosine fourier transform of f(x)=1/√x is: √(2/πω)=√(/π²f). (Source: http://www.math.uni-sb.de/ag/fuchs/PDE14-15/pde14-15-lecture-7.pdf)
When f=0.1 Hz, Fourier transform is √(/π²*f)=1.007. What does it really mean? In my opinion, it means that the magnitude of cosine sinusoid with frequency 0.1 Hz which combines with other cosine sinusoids to make up the signal f(x)=1/√x is 1.007. Do I have it correct? Thank you.
What does it really mean? In my opinion, it means that the magnitude of cosine sinusoid with frequency 0.1 Hz which combines with other cosine sinusoids to make up the signal f(x)=1/√x is 1.007. Do I have it correct? Thank you.
The second expression makes no sense.
Also, when you take the square root of pi squared you get pi:
1/(pi*sqrt(f))
the expression /pi^2*f does not make sense on several levels.
Thank you, MrAl.
Are you talking about inverse fourier transform expression?
Sorry, I meant to write cosine fourier transform of f(x)=1/√x is: √(2/πω)=√(1/π²f)=1/π*√(1/f).
By the way, you may want to look up "completeness" for the Fourier-cosine transform. I have a vague recollection that this is not a complete transform and may only be complete for "even" functions. Normally, the full Fourier transform with has both sine and cosine components is considered to be complete.
Perhaps one of the things that protects you here is that the integration limits are only on the positive part of the x-axis and not on the negative part. This converts your function (effectively) into an even function by ignoring the part in the negative domain. If your function is not even, or if the function is undefined in the negative domain, then you never see the problem from this fact.
%file name fourier.m
clear all; close all; clc;
f = [0.001]; %defining and initializing a constant vector
%0 Hz to 100 Hz, with 0.1 step, there would be 1000 frequencies
cos_v=0.*[1:50000000]'; %defining and initializing vector to store values
%a vector with 1000 zeroes
f_w=[0]; % defining and initializing a vector, fourier transform,
user_decision = 'y';
while user_decision == 'y'
x = input('Enter value of x: ');
for i1=[1:50000000] % for loops begins, with loop index i1
f_w = (1/pi)*sqrt(1/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.001; %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;
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(x))))
user_decision=input('Would you like to continue, enter Y/N: ','s');
if user_decision=='n'
break
end
end
Enter value of x: 2
The value of combined_sinusoids is 9.784050e+01
The value of 1/sqrt(x) is 7.071068e-01
Difference between 1/sqrt(x) and approximation by combined_sinusoids is 9.713339e+01
Would you like to continue, enter Y/N: n
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?