0

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.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
  • Can you mark the lines 43 and 52 in your code? – frederick99 Feb 25 '17 at 21:04
  • Matlab will automatically expand a matrix or vector when assigning an otherwise out of bounds value. For example, `t=[1, 2];` `t(3) = 3;` gives `t=[1, 2, 3]`. The line `y3(k+1) = N/2;` will always assign a tenth value in the last loop for `k=9` – Maurits Feb 25 '17 at 21:07
  • For line 43, it says line 43 inside the actual `stem` definition. Not in the code itself. –  Feb 25 '17 at 21:09

1 Answers1

0
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);

smttsp
  • 4,011
  • 3
  • 33
  • 62
  • Hmm, so if my `H(e^{...})` has the following values for a period of 9, can I just simply have the following? `y3 = [1 1 1 0 0 0 0 1 1]; x3 = ifft(y3);` –  Feb 25 '17 at 21:16
  • Is `y3` in frequency domain or space? – smttsp Feb 25 '17 at 21:20
  • `y3` is frequency domain –  Feb 25 '17 at 21:48
  • If you are sure `ifft` is what you want to do, you can simply do that: `x3 = ifft(y3);` The syntax and the idea are correct based on your explanation. – smttsp Feb 25 '17 at 21:52