- #1
PainterGuy
- 940
- 70
- TL;DR Summary
- I have been trying to write a MATLAB code to approximate an inverse trigonometric Fourier transform of unit pulse using Riemann sum. My code isn't working.
Hi,
Although I'm using trigonometric form of Fourier transform, first I'd discuss both, exponential and trigonometric forms, for the sake of context.
Now proceeding toward the main question and we would only be using trigonometric form.
I understand that the code above involves many iterations so to simplify it just to see if it works, I reduced the number of iterations. It doesn't work. I have include the errors at the bottom. Could you please help me to fix the code? Thank you!
Although I'm using trigonometric form of Fourier transform, first I'd discuss both, exponential and trigonometric forms, for the sake of context.
Now proceeding toward the main question and we would only be using trigonometric form.
Matlab:
% file name unit_pulse_fourier_transform_riemann_sum.m
% riemann sum: https://www.math.hmc.edu/calculus/tutorials/riemann_sums/gif/figure3.gif
clear all; close all; clc;
Dw=0.5; %delta Omega
i=1; %frequency increases 0 to 500 in steps of 0.5 Hz therefore max i should be 1000;
w=0.*[0:0.5:500];
w_mid=zeros(1000);
x=linspace(-2*pi,2*pi,13000); % dividing the interval into 13000 points
Rs=zeros(1000);
Rs_x=zeros(13000);
for i1=1:13000
w(1)=0;
w(2)=0.5;
for i=1:1000
w_mid(i)=(w(i+1) + w(i))/2 %w_mid(1) is first middle point
Rs(i)=(2*sin(pi*w_mid(i))/w_mid(i))*cos(x(i1)*w_mid(i))*Dw;
w(i+2)=w(i+1) + Dw;
end
Rs_x(i1)=sum(Rs); % Rs_x holds values for evaluated Rs
end
plot(x,Rs_x);
hold;
Matlab:
% file name unit_pulse_fourier_transform_riemann_sum.m
% for riemann sum: https://www.math.hmc.edu/calculus/tutorials/riemann_sums/gif/figure3.gif
clear all; close all; clc;
Dw=0.5;
w=0.*[0:0.5:25];
w_mid=zeros(50);
x=linspace(-2*pi,2*pi,60);
Rs=zeros(50);
Rs_x=zeros(60);
for i1=1:60
w(1)=0;
w(2)=0.5;
for i=1:50
w_mid(i)=(w(i+1) + w(i))/2 %w_mid(1) is first middle point
Rs(i)=(2*sin(pi*w_mid(i))/w_mid(i))*cos(x(i1)*w_mid(i))*Dw;
w(i+2)=w(i+1) + Dw;
end
Rs_x(i1)=sum(Rs);
end
plot(x,Rs_x);
hold;
Code:
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in delete_fourier_pulse (line 28)
Rs_x(i1)=sum(Rs);
Last edited: