matlabmatrixfftifft

Unable to generate coefficient matrix in MATLAB


I have the following piece of code and I am trying to calculate the coefficient matrix, a_k to solve the linear system to obtain h[n], the impulse response. I am using the inverse fast Fourier transform.

N = 9; % period is chosen to be 16
n = 0:N-1; %vector for x must start at n = 0
for k = 1:9
    y3 = zeros(1,9);
    y3(k+1) = N/2;
    y3(N - k + 1) = N/2;  
end
x3 = ifft(y3);

figure;
subplot(2,2,1);stem(n,real(x3));xlabel('n'); //line 52
ylabel('real(x3)');axis([0 N-1 -1 1]);
subplot(2,2,2);stem(n,imag(x3));xlabel('n');
ylabel('imag(x3)');axis([0 N-1 -1 1]);
subplot(2,2,3);stem(n,real(y3)/N);xlabel('k');
ylabel('real(a_k)');axis([0 N-1 -1 1]);
subplot(2,2,4);stem(n,imag(y3)/N);xlabel('k');
ylabel('imag(a_k)');axis([0 N-1 -1 1]);

However, when I run this code, I get the following error:

Error using stem (line 43)
X must be same length as Y.

Error in fft_examples (line 52)
subplot(2,2,1);stem(n,real(x3));xlabel('n');

I'm not sure where I am erring. I know that the matrix of k is from 1 through 9. Hence, I did a for loop. The y values are becoming mismatched.


Solution

  • size(real(x3))  % --> 1 10
    size(n)         % --> 1 9
    

    So they are not the same size. You are increasing the size of y3 in y3(k+1) = N/2;

    Also, why would you want to create matrix y3 in every iteration: y3 = zeros(1,9);