Thread: General MATLAB question

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    210

    General MATLAB question

    Since matlab and C have somewhat similarities I was wondering if you guys could help me. We have this problem in our class and I am having difficulty fully understanding the code:

    Code:
      %Implementing IIR Direct Form II Filter
      
      n=1:100;
      w = 2*pi*1600;
      f_s = 8000;  %the sampling rate for the system
      T = 1/f_s;    % period
      
      f = @(x)cos(w*T*(x-1)); 
      
      %the coefficient for the first system 
      b0_1 = 0.292893;
      b1_1 = 0.585786;
      b2_1 = 0.292893;
      
      a1_1 = 0.91018;
      a2_1 = 0.414213;
      
      %the coefficient for the second system
      b0_2 = 1;
      b1_2 = -2;
      b2_2 = 1;
      a1_2 = -0.91018;
      a2_2 = 0.414214;
      
      
      
      %calculate initial conditions for y1
      y1(1) = b0_1*f(0) + b1_1*f(-1) + b2_1*f(-2);
      y1(2) = b0_1*f(1) + b1_1*f(0) + b2_1*f(-1) - a1_1*y1(1);
      
      for m = 3:100   
          y1(m) = b0_1*f(m) + b1_1*f(m-1) + b2_1*f(m-2) - a1_1*y1(m-1) - a2_1*y1(m-2);
      end
      
      %initial conditions for y2
      y2(1) = b0_2*y1(1);
      y2(2) = b0_2*y1(2) + b1_2*y1(1) - a1_2*y2(1);
      
      
      for p = 3:100
          y2(p) = b0_2*y1(p) + b1_2*y1(p-1) + b2_2*y1(p-2) - a1_2*y2(p-1) - a2_2*y2(p-2);
      end
      plot(y2)
      %stem(n,y2)
    The equations inside the for loop and their coefficients are already a part of the question.
    I don't understand what the initial conditions are doing?
    This code is correct since it is given to us.

    The basic problem is:
    Let the input be x[n] = cos(wnT). Let w = 2pi× 1600. Write a MATLAB program to compute the output y[n].

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Just FYI, the similarities between C and Matlab are quite small. But many people on this board have EE backgrounds and know Matlab. I'm rusty, but hope this helps.

    Take a look at the image here: Digital biquad filter - Wikipedia, the free encyclopedia. Those z-1 blocks are unit delays, meaning they use the sample from one period before for the calculation. The image is described by the equations underneath:

    y[n] = b0ω[n] + b1ω[n-1] + b2ω[n-2]
    Where
    ω[n] = x[n] + a1ω[n-1] + a2ω[n-2]

    So for y[0], you would need to know ω[-1] and ω[-2] (and ω[-2] would require ω[-3] and ω[-4]). Since you don't have anything before x[0] and ω[0], those statements are pre-filling y[0] and y[1].
    Last edited by anduril462; 11-05-2013 at 04:19 PM.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    210
    Thank you I think I kind of get it.
    I am trying to plot the theoretical amplitude response, but I am getting an error on Matlab. I've been staring at the screen and working on this for the past one and a half our please help me out

    It says Warning: Imaginary Parts of complex X and or Y arguments ignored.


    Code:
    
    f_sample = 8000;  
    T = 1/f_sample;    
    
    b0_1 = 0.292893;
    b1_1 = 0.585786;
    b2_1 = 0.292893;
    
    a1_1 = 0.91018;
    a2_1 = 0.414213;
    
    
    b0_2 = 1;
    b1_2 = -2;
    b2_2 = 1;
    a1_2 = -0.91018;
    a2_2 = 0.414214;
    for k=0:4000;
        w = 2*pi*k;
     
     
      A = [(b0_1 + (b1_1*(exp((-1i)*(w/8000)))) + (b2_1*(exp((-1i)*(2*w/8000)))))]/[(1+(a1_1*(exp((-1i)*(w/8000)))) + (a2_1*(exp((-1i)*((2*w)/8000)))))] * [(b0_2 + (b1_2*(exp((-1i)*(w/8000)))) + (b2_2*(exp((-1i)*(2*w/8000)))))]/[(1+(a1_2*(exp((-1i)*(w/8000)))) + (a2_2*(exp((-1i)*((2*w)/8000)))))];
       
        
        plot(f,A)
    end

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    It would help if you pointed out the line causing the problem. In any case, it's almost certainly the plot function. I don't see a definition of f in your new code, is it the same as in post 1? Note that you keep using -1i when you calculate A, so A has imaginary parts.

    I don't have a copy of Matlab, so not sure how much I can help you. For most problems, if you Google the error message, you often find lots of existing help. In fact, that is all I did, and I got the following results: https://www.google.com/search?q="Warning%3A+Imaginary+Parts+of+complex+X+ and+or+Y+arguments+ignored.".

    It's pretty self explanatory, since A has imaginary parts, the plot function is warning you that it will not graph those. One of the links suggests using abs() to convert all complex numbers to magnitudes. It will probably remove the warning, but it may make your graph incorrect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. general question
    By ingeniousreader in forum C Programming
    Replies: 4
    Last Post: 05-21-2012, 09:59 AM
  2. General question
    By stefanyco in forum Tech Board
    Replies: 2
    Last Post: 10-21-2011, 01:47 AM
  3. General API Question
    By legion050 in forum C Programming
    Replies: 2
    Last Post: 11-23-2010, 06:00 AM
  4. General Question
    By obaid in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 08-22-2007, 05:39 AM
  5. General GUI question in C
    By Music_Man in forum Game Programming
    Replies: 3
    Last Post: 11-16-2001, 11:45 AM