0

I have a code that yields a solution similar to the desired output, and I don't know how to perfect this.

The code is as follows.

N = 4; % sampling period
for nB = -30:-1; 
  if rem(nB,N)==0
    xnB(abs(nB)) = -(cos(.1*pi*nB)-(4*sin(.2*pi*nB))); 
  else
    xnB(abs(nB)) = 0;
  end
end
for nC = 1:30; 
  if rem(nC,N)==0
    xnC(nC) = cos(.1*pi*nC)-(4*sin(.2*pi*nC)); 
  else
    xnC(nC) = 0;
  end
end
nB = -30:-1; 
nC = 1:30;
nD = 0;
xnD = 0;
plot(nA,xnA,nB,xnB,'r--o',nC,xnC,'r--o',nD,xnD,'r--o')

This produces something that is close, but not close enough for proper data recovery.

I have tried using an index that has the same length but simply starts at 1 but the output was even worse than this, though if that is a viable option please explain thoroughly, how it should be done.

I have tried running this in a single for-loop with one if-statement but there is a problem when the counter passes zero. What is a way around this that would allow me to avoid using two for-loops? (I'm fairly confident that, solving this issue would increase the accuracy of my output enough to successfully recover the signal.)

EDIT/CLARIFICATION/ADD - 1

I do in fact want to evaluate the signal at the index of zero. The if-statement cannot handle an index of zero which is an index that I'd prefer not to skip.

The goal of this code is to be able to sample a signal, and then I will build a code that will put it through a recovery filter.

EDIT/UPDATE - 2

nA = -30:.1:30; % n values for original function
xnA = cos(.1*pi*nA)-(4*sin(.2*pi*nA)); % original function
N = 4; % sampling period
n = -30:30;
xn = zeros(size(n));
xn(rem(n,N)==0) = -(cos(.1*pi*n)-(4*sin(.2*pi*n)));
plot(nA,xnA,n,xn,'r--o')
title('Original seq. x and Sampled seq. xp')
xlabel('n')
ylabel('x(n) and xp(n)')
legend('original','sampled');

This threw an error at the line xn(rem(n,N)==0) = -(cos(.1*pi*n)-(4*sin(.2*pi*n))); which read: In an assignment A(I) = B, the number of elements in B and I must be the same. I have ran into this error before, but my previous encounters were usually the result of faulty looping. Could someone point out why it isn't working this time?

EDIT/Clarification - 3

N = 4; % sampling period
for nB = -30:30;
    if rem(nB,N)==0
        xnB(abs(nB)) = -(cos(.1*pi*nB)-(4*sin(.2*pi*nB)));
    else
        xnB(abs(nB)) = 0; 
    end
end

The error message resulting is as follows: Attempted to access xnB(0); index must be a positive integer or logical.

EDIT/SUCCESS - 4

After taking another look at the answers posted, I realized that the negative sign in front of the cos function wasn't supposed to be in the original coding.

SolidusVerum
  • 123
  • 7

2 Answers2

3

From what I understand, you want to iterate over all integer values in [-30, 30] excluding 0 using a single for loop. this can be easily done as:

for ii = [-30:-1,1:30]
    %Your code
end

Resolution for edit - 2

As per your updated code, try replacing

xn(rem(n,N)==0) = -(cos(.1*pi*n)-(4*sin(.2*pi*n)));

with

xn(rem(n,N)==0) = -(cos(.1*pi*n(rem(n,N)==0))-(4*sin(.2*pi*n(rem(n,N)==0))));

This should fix the dimension mismatch.

Resolution for edit - 3

Try:

N = 4; % sampling period
for nB = -30:30;
    if rem(nB,N)==0
        xnB(nB-(-30)+1) = -(cos(.1*pi*nB)-(4*sin(.2*pi*nB)));
    else
        xnB(nB-(-30)+1) = 0; 
    end
end
Roney Michael
  • 3,964
  • 5
  • 30
  • 45
  • I will edit my question to clarify I want to include the zero value, do you know how to do that? – SolidusVerum Dec 02 '13 at 05:56
  • @SolidusVerum: Could you please clarify? Why is the `if` statement supposedly flawed here? What do you expect if the loop itinerant is zero? – Roney Michael Dec 02 '13 at 06:03
  • When I try and put `nb` and `nc` together into one variable and use the `if` statement in the loop it works until the index comes to zero and then matlab stops the loop. – SolidusVerum Dec 02 '13 at 06:14
  • @SolidusVerum: That should not happen as far as I can see. Please post the exact code that failed and error messages if any. – Roney Michael Dec 02 '13 at 06:19
  • The code I tried was this: ... well here it is I'll just put this in the original question to for ease of viewing because I can't figure out line quotes. >N = 4; % sampling period >for nB = -30:30; >if rem(nB,N)==0 >xnB(abs(nB)) = -(cos(.1*pi*nB)-(4*sin(.2*pi*nB))); >else >xnB(abs(nB)) = 0; >end >end The error message that resulted was this: **Attempted to access xnB(0); index must be a positive integer or logical.** – SolidusVerum Dec 02 '13 at 06:24
  • @SolidusVerum: The edit I just posted should resolve your issue. – Roney Michael Dec 02 '13 at 06:27
  • I have just tried to run the updated code in total and the for some reason the output is still off, most of the `xn` values are zero. – SolidusVerum Dec 02 '13 at 06:34
  • @SolidusVerum: The values of `xn` obtained would zero because of the logic involved. The code should be perfectly fine from a syntactical view-point. – Roney Michael Dec 02 '13 at 06:36
  • I just tried the updated resolution-3 and unfortunately the results are not being plotted correctly. ![All of the plots are along side the far axis exactly where they shouldn't be.](http://imgur.com/r3s0FVL) – SolidusVerum Dec 02 '13 at 06:46
  • @SolidusVerum: Hi, sorry, but like I said, this would be an issue specific to logic used. It would not be a coding issue. – Roney Michael Dec 02 '13 at 07:06
3

You could do something like the following:

nB = -30:1
nC = 1:30
xnB = zeros(size(nB));
remB = rem(nB,N)==0;
xnB(remB) = -cos(.1*pi*nB(remB))-(4*sin(.2*pi*nB(remB));
xnC = zeros(size(nC));
remC = rem(nC,N)==0;
xnC(remC) = cos(.1*pi*nC(remC))-(4*sin(.2*pi*nC(remC)));

This avoids the issue of having for-loops entirely. However, this would produce the exact same output as you had before, so I'm not sure that it would fix your initial problem...

EDIT for your most recent addition:

nB = -30:30;
xnB = zeros(size(nB));
remB = rem(nB,N)==0;
xnB(remB) = -(cos(.1*pi*nB(remB))-(4*sin(.2*pi*nB(remB)));

In your original post you had the sign dependent on the sign of nB - if you wanted to maintain this functionality, you would do the following:

xnB(remB) = sign(nB(remB).*(cos(.1*pi*nB(remB))-(4*sin(.2*pi*nB(remB)));
MrAzzaman
  • 4,734
  • 12
  • 25
  • I've just tried to add the variables `nb` and `nc` together into one variable `n` unfortunately it is throwing an error that `**In an assignment A(I) = B, the number of elements in B and I must be the same.**` I'll edit my question to show you what I tried. – SolidusVerum Dec 02 '13 at 06:16
  • @SolidusVerum: Try `xnB(rem(nB,N)==0) = -cos(.1*pi*nB(rem(nB,N)==0))-(4*sin(.2*pi*nB(rem(nB,N)==0)));` and `xnC(rem(nC,N)==0) = cos(.1*pi*nC(rem(nC,N)==0))-(4*sin(.2*pi*nC(rem(nC,N)==0)));` instead. – Roney Michael Dec 02 '13 at 06:22
  • Whoops, sorry. Oversight on my part. – MrAzzaman Dec 02 '13 at 06:36
  • @RoneyMichael Even utilizing this updated code still doesn't get past the fact that zero isn't used. – SolidusVerum Dec 02 '13 at 06:39
  • @SolidusVerum: Please post any error messages you get. Saying that the code does not pass is not sufficient. – Roney Michael Dec 02 '13 at 06:40
  • @SolidusVerum I have updated my answer to reflect your most recent additions to the question – MrAzzaman Dec 02 '13 at 06:43
  • @RoneyMichael It's not that there is an error message its that the plot isn't coming out correctly. The plot was similar to the plot I recently posted int a comment on your answer thread. – SolidusVerum Dec 02 '13 at 06:49