Removing Self-Referencing and Reference of Outside Terms From function_handle Summation

Hello, I am trying to write a program to perform a DTFT, and I am trying to make it as general as possible, even creating the equation for Am automatically by just changing the input matrix. However, when adding each new term to A1 (based on x1, the first set of discrete values entered), the function self-references. Is there a way to perform this operation without having the self-referencing?
Additionally, it was discovered that the 'k' term remains in the function as well, and I cannot have that either. Is there a way to solve that issue as well?
The current symptom of the system is that when I type A1(0), I get out the input matrix. Up until the length of x1, but it is the input matrix.
%Initial Discrete Set, in this case starting at n = 0.
x1 = [1,2,3,2,1]; %This should be assumed as inconsistent, and it could possibly vary.
%I want the below code to be as universal as possible.
start = 0; %Value of n for the FIRST index of x1; again, user specified, but set here.
M = 6 %Desired number of DTFT terms; also user specified.
N = length(x1);
stem(0:1:N - 1,x1);
xlabel('n');
ylabel('x1(n)');
title('x1(n)');
%Calculate DTFT, completely based off values above, assuming the above is inconsistent.
for k = 0:N - 1
A1 = @(m) A1(m) + x1(k + 1 - start)*exp(1i*2*pi*(k - start)*m/N); %Each term is: x1(n)*exp(i*2*pi*m*n/N)
end
Am1 = zeros(1,M);
for j = 0:M - 1
Am1(j + 1) = A1(j);
end
%The first for loop is the issue; the second merely calculates the DTFT coefficients based off the formula in the first loop.
This is not for any deadline, so expediancy is not vital. I am merely trying to do this to become more proficient at MATLAB for a possible future endeavour.
Thank You,
Parry Hayes

1 Comment

To get away from the self-referencing, do not write the code as an anonymous function: write it as a call to a function that loops as needed.
Or use the symbolic toolbox to generate the complete symbolic expression.

Sign in to comment.

Answers (0)

Products

Release

R2019a

Asked:

on 9 Oct 2019

Edited:

on 9 Oct 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!