0
votes

I want to plot the Fourier transform, using MATLAB, of data which is plotted with the following code:

x = -2:0.01:2; % position vector
s = @(x)heaviside(x+1).*heaviside(-x)+heaviside(x).*heaviside(1-x); % Step    
function between -1 and 1
phi = @(x) exp(-1./(1-x.^2)).*s(x); % Test function
d = @(t) integral(@(x)phi(x),0,t); % Data
fplot(d,[0,1])

However, none of the MATLAB tutorials seem to be useful in plotting a fast Fourier transform of d, which is defined in terms of a variable integral.

If I try with

Fs = 1000;            % Sampling frequency
T = 1/Fs;             % Sampling period
L = 1000;             % Length of signal
t = (0:L-1)*T;        % Time vector
s = @(x)heaviside(x+1).*heaviside(-x)+heaviside(x).*heaviside(1-x); % Step     
function
phi = @(x) exp(-1./(1-x.^2)).*s(x); % Test function
d = @(t) integral(@(x)phi(x),0,t); %Data
plot(1000*t(1:50),d(1:50))

Then I get the following error:

Error using integral
A and B must be floating-point scalars.

Error in @(t)integral(@(x)phi(x),0,t)

I don't know how to fix this though.

1

1 Answers

1
votes

The error happens because integrate's second and third arguments (the lower and upper bounds for the integration) must be scalars. To get a vector of values, with which to compute the fft you need to compute the element's values in a for loop

d = zeros(size(t));
for i = 1:length(t)
    d(i) = integral(phi,0,t(i));
end

Then, you will be able to plot t versus d and also compute the fft of d by calling

y = fft(d);

Checkout the examples in the docs to see how to plot the results that fft returns.