1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Working of Digital filters...

Discussion in 'Homework Help' started by koolguy, Oct 31, 2011.

  1. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Hi,
    Ia m using X(n) 1 2 3 4 (for 0 to 3) and H(n) 1 2 3 4 (for -1 to 2)
     
  2. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Hi,
    Ia m using X(n) 1 2 3 4 (for 0 to 3) and H(n) 1 2 3 4 (for -1 to 2)
     
  3. MrAl

    MrAl Well-Known Member Most Helpful Member

    Joined:
    Sep 7, 2008
    Messages:
    11,026
    Likes:
    951
    Location:
    NJ
    Hi,

    Ok, and what is your output sequence?
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9

    OK, it working fine the issue was i was not clear about the first program after using 2nd add. program it is giving right answer...........
     
  6. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Using y(n)=x(k)*h(n-k)= gives 1 2 4 4 3 2 1 from (n=-1 to 5)
     
  7. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    trying using alternative by fig. calculation by shifting the sequence in -ve and +ve direction and multiplying the sample of amplitude one give totally diff. answer....
     

    Attached Files:

    • 1.JPG
      File size:
      72 KB
      Views:
      153
  8. MrAl

    MrAl Well-Known Member Most Helpful Member

    Joined:
    Sep 7, 2008
    Messages:
    11,026
    Likes:
    951
    Location:
    NJ
    Hi,

    Sometimes you have to sit down and as they say, "roll your sleeves up" and write down every detail until you see what is not working.
    We should do that here and i think after we do some of this you'll see how easy this is. All we are doing is multiplication and addition and arranging things in a certain order.

    Lets do this the long way first, according to the definition sum for all k of x[k]*h[n-k].
    Well start with the calculation of the very first sample of y[n].
    So starting with n=-1, we tabulate the entire process by enumerating by k and finding x[k] and h[n-k] and the product x*h for each k:

    Code (text):

    This is for n=-1

           x[k]  h[n-k]  Product
    k=-1    0     2        0
    k=0     1     1        1
    k=1     2     0        0
    k=2     3     0        0
    k=3     4     0        0
    result sum:            1

    (the result sum is just the sum of all values in the last column)

     
    So the very first sample of y[n] is equal to 1.

    Now lets do n=0:
    Code (text):

    This is for n=0

           x[k]  h[n-k]  Product
    k=-1    0     3        0
    k=0     1     2        2
    k=1     2     1        2
    k=2     3     0        0
    k=3     4     0        0
    result sum:            4
     
    So the next output sample is equal to 4, so now we have two samples of the output:
    y[n]={1,4}

    with more to come.

    Does this make sense now?

    You should try to get the next several samples until they all start to equal zero.
     
    Last edited: Nov 20, 2011
    • Like Like x 1
  9. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Hi again,

    If we will try like this looking at samples and shifting sequence ...........

    printf(1,"%s\n",{"gdfghdsg"})
    while 1 do
    end while


    and what the use of this bold letter in program changing the no. shows an error?
     

    Attached Files:

    • 12.JPG
      File size:
      74.4 KB
      Views:
      109
    Last edited: Nov 20, 2011
  10. MrAl

    MrAl Well-Known Member Most Helpful Member

    Joined:
    Sep 7, 2008
    Messages:
    11,026
    Likes:
    951
    Location:
    NJ
    Hi,

    I have no idea what you are talking about, and i can not read your attachment at all.
    You are going to have to pose your questions in more detail, and also use something else to make your graphic attachments.
     
    • Like Like x 1
  11. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Hi again,

    Please explain this instruction use in program....
    why there is 1 and while condition??

    and in this post........
    you say that choose the value of h(n) as low pass filter but how??
    as in case of low pass RC filter we just put the value of R and C according to formula but here how to work in this??
     
  12. MrAl

    MrAl Well-Known Member Most Helpful Member

    Joined:
    Sep 7, 2008
    Messages:
    11,026
    Likes:
    951
    Location:
    NJ
    Hi,

    In the print statement, "%s" says that we want to use a text string (which is later as you typed "g..."
    The "\n" says we want the display to go to the next line after that "g..." prints on the screeen.
    The "while 1 do, end while" keeps the program running indefinitely until you manually close out the display window (little "X"). This is to prevent the program from closing immediately which would not give you time to see the "g..." print on the screen :)

    Yes, in the analog design we just choose the values of R and C and that makes the filter, but with the digital design we can also choose R and C, but we dont actually use a resistor and capacitor, we use a unit sample response of the filter or alternately we use a difference equation. Knowing what R and C are allows us to design a digital filter with similar characteristics of the analog filter.
    So to get h(n) we could derive the H(z) knowing the R and C, then derive the h(n) knowing the H(z). Does that make sense so far?

    I think you might be better off starting with a design where you use a difference equation rather than impulse or unit sample response. A difference equation allows you to figure out the whole filter without needing to convolve anything later. I think we should do that first, and it wont be hard to do at all.

    One procedure for obtaining the discrete time impulse response would be to:
    Start with H(s).
    Calculate the inverse Laplace Transform of H(s), h(t)=L^-1(H(s)), h(t) is the continuous time impulse response.
    Integrate h(t) from kT-T to kT, obtaining a formula for h[k].
    Use that to calculate the sample values h[k]* of h[k] from k=1 to k=whatever.
    The only problem now is that the resulting samples of h[k] must be convolved with the input in continuous time.
    Using a difference equation we get around that.
     
    Last edited: Nov 22, 2011
    • Like Like x 1
  13. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Hi,

    pls elaborate more with brief example....
    as why we are using H(z) now??
    Sure .......
     
  14. MrAl

    MrAl Well-Known Member Most Helpful Member

    Joined:
    Sep 7, 2008
    Messages:
    11,026
    Likes:
    951
    Location:
    NJ
    Hi,

    As i said once, there are several ways to do it. The last method i give was a direct method. The previous method used H(z). So that's two different methods.
    I also mentioned the different equation, because with that you can just program a uC and then pump in the values from the ADC or something like that, without
    actually doing any kind of convolution calculation. It seems to make life easier.

    We should do a couple examples next. I guess we can do both kinds of sampling methods, using the discrete time impulse response and using a difference equation, and of course using a continuous time system, then compare results. Should be interesting right?
    You should be doing this yourself too, or at least getting ready to do some.
     
    • Like Like x 1
  15. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Hi again,

    Yes it will interesting to do............
     
  16. MrAl

    MrAl Well-Known Member Most Helpful Member

    Joined:
    Sep 7, 2008
    Messages:
    11,026
    Likes:
    951
    Location:
    NJ
    Hi again,

    First we'll do a difference equation. This one will be for a 2nd order low pass filter derived from a 2nd order analog low pass filter with two R's and two C's, and an amplifier with gain of 1 in between the two RC sections. This makes the transfer function:
    H(s)=a^2/(s+a^2) where a is 1/RC, which is easy to work with.

    First we have to transform the transfer function into a digital form. To do this, we will use the Bilinear Transform which is:
    (2*(z-1))/((z+1)*T)
    Note that there are variants of this transform but this works ok so we'll stick with this for now.

    Step by Step:

    Each sub RC section is H1:
    H1=a/(s+a), a=1/RC

    The 2nd order filter is the same two RC sections convolved together with the amplifier with gain=1:
    H(s)=H1^2=a^2/(s+a)^2

    With input Vi we get:
    H(s)=Vin*a^2/(s+a)^2

    Using the Bilinear Transform we get:
    H(z)=Vin*a^2/((2*(z-1))/((z+1)*T)+a)^2

    Simplified:
    H(z)=Vin*(a^2(z+1)^2*T^2)/(a*z*T+a*T+2*z-2)^2

    or in sampling notation:
    yn=xn*(a^2(z+1)^2*T^2)/(a*z*T+a*T+2*z-2)^2

    Multiply both sides by the denominator:
    yn*(a*z*T+a*T+2*z-2)^2=a^2*xn*(z+1)^2*T^2

    Expand that out:
    a^2*yn*z^2*T^2+2*a^2*yn*z*T^2+a^2*yn*T^2+4*a*yn*z^2*T-4*a*yn*T+4*yn*z^2-8*yn*z+4*yn=a^2*xn*z^2*T^2+2*a^2*xn*z*T^2+a^2*xn*T^2

    Divide both sides by the highest power of z:
    (2*a^2*yn*T^2)/z+(a^2*yn*T^2)/z^2+a^2*yn*T^2-(4*a*yn*T)/z^2+4*a*yn*T-(8*yn)/z+(4*yn)/z^2+4*yn=(2*a^2*xn*T^2)/z+(a^2*xn*T^2)/z^2+a^2*xn*T^2
    Note now z does not appear in any numerator, only in the denominators, and that we left xn and yn with no z denominators alone.

    Next we replace every occurance of xn/z with xn1, xn/z^2 with xn2, and yn/z with yn1, and yn/z^2 with yn2:
    a^2*yn2*T^2+2*a^2*yn1*T^2+a^2*yn*T^2-4*a*yn2*T+4*a*yn*T+4*yn2-8*yn1+4*yn=a^2*xn2*T^2+2*a^2*xn1*T^2+a^2*xn*T^2
    You have to be careful there to get the xn and yn correct or it wont work right later.

    We get:
    yn=(-a^2*yn2*T^2-2*a^2*yn1*T^2+a^2*xn2*T^2+2*a^2*xn1*T^2+a^2*xn*T^2+4*a*yn2*T-4*yn2+8*yn1)/(a^2*T^2+4*a*T+4)

    Next we replace every xn with x[n], yn with y[n], xn1 with x[n-1], xn2 with x[n-2], yn1 with y[n-1], yn2 with y[n-2]:
    y[n]=(a^2*x[n]*T^2-2*a^2*y[n-1]*T^2+2*a^2*x[n-1]*T^2-a^2*y[n-2]*T^2+a^2*x[n-2]*T^2+4*a*y[n-2]*T+8*y[n-1]-4*y[n-2])/(a^2*T^2+4*a*T+4)

    And that leaves us with our difference equation.

    Now we come to the testing phase. We'll compare this equation to the equivalent analog filter described above as H(s) using the unit step input.

    First we have to calculate some values of the output yn with xn being the unit step, so we write a program...

    Here is the program:
    Code (text):


    --This program calculates the sucessive values of the output of a 2nd order digital low pass filter with a unit step input.
    --H(s)=a^2/(s+a)^2


    include misc.e --include various useful functions
    include file.e --include file operation functions


    atom R,C,T,T2,a,a2,yn --declare variables to be used
    sequence x,y --declare arrays to be used

    x=repeat(1,1000) --declare x to be a unit step input with 1000 samples equaling 10 seconds with T=0.01 seconds
    y=repeat(0,length(x)+2) --declare an array for the output values, y[n]

    R=1 --the resistor value of the analog filter
    C=1 --the capacitor value of the analog filter
    T=0.01 --the sampling time period for the digital filter
    a=1/(R*C) --the constant a

    a2=a*a --square of a
    T2=T*T --square of T

    --declare a function to calculate successive values of yn:
    function NextY(integer n)
      --original difference equation:
      --y[n]=(a^2*x[n]*T^2-2*a^2*y[n-1]*T^2+2*a^2*x[n-1]*T^2-a^2*y[n-2]*T^2+a^2*x[n-2]*T^2+4*a*y[n-2]*T+8*y[n-1]-4*y[n-2])/(a^2*T^2+4*a*T+4)

      --difference equation with squared values as a2, T2, etc.:
      y[n]=(a2*x[n]*T2-2*a2*y[n-1]*T2+2*a2*x[n-1]*T2-a2*y[n-2]*T2+a2*x[n-2]*T2+4*a*y[n-2]*T+8*y[n-1]-4*y[n-2])/(a2*T2+4*a*T+4)
      return y[n]
    end function

    --In the following loop we have to start at 3 so we dont try to pluck values from the arrays with indexes less than 1.
    --Use a loop to do each n starting from x[3] and ending with the last x[n]:
    for n=3 to length(x) do
      yn=NextY(n) --calc next yn
      if n/10=floor(n/10) then --display every 10th value only
        printf(1,"(%2.2f,%f)\n",{n*T,yn}) --print time and output as pairs: (t,yn)
      end if
    end for


    sleep(120) --keep the program window open for 120 seconds so the user can examine the data

     
    The program allows us to calculate a lot of output values really fast, so we use that instead of doing it by hand. We could also use a hand calculate or better yet a programmable calculator. The choice is yours.

    The output values obtained by the program for every 0.1 second from 0.1 to 10 seconds are as follows:

    Code (text):


    (0.10,0.003402)
    (0.20,0.015131)
    (0.30,0.033652)
    (0.40,0.057567)
    (0.50,0.085682)
    (0.60,0.116980)
    (0.70,0.150602)
    (0.80,0.185821)
    (0.90,0.222030)
    (1.00,0.258720)
    (1.10,0.295472)
    (1.20,0.331943)
    (1.30,0.367852)
    (1.40,0.402977)
    (1.50,0.437142)
    (1.60,0.470210)
    (1.70,0.502082)
    (1.80,0.532686)
    (1.90,0.561975)
    (2.00,0.589921)
    (2.10,0.616514)
    (2.20,0.641760)
    (2.30,0.665674)
    (2.40,0.688281)
    (2.50,0.709613)
    (2.60,0.729707)
    (2.70,0.748607)
    (2.80,0.766358)
    (2.90,0.783007)
    (3.00,0.798603)
    (3.10,0.813195)
    (3.20,0.826834)
    (3.30,0.839569)
    (3.40,0.851449)
    (3.50,0.862520)
    (3.60,0.872830)
    (3.70,0.882422)
    (3.80,0.891340)
    (3.90,0.899626)
    (4.00,0.907319)
    (4.10,0.914456)
    (4.20,0.921075)
    (4.30,0.927208)
    (4.40,0.932889)
    (4.50,0.938148)
    (4.60,0.943014)
    (4.70,0.947513)
    (4.80,0.951673)
    (4.90,0.955516)
    (5.00,0.959065)
    (5.10,0.962342)
    (5.20,0.965365)
    (5.30,0.968155)
    (5.40,0.970727)
    (5.50,0.973098)
    (5.60,0.975282)
    (5.70,0.977295)
    (5.80,0.979148)
    (5.90,0.980855)
    (6.00,0.982425)
    (6.10,0.983870)
    (6.20,0.985199)
    (6.30,0.986421)
    (6.40,0.987544)
    (6.50,0.988577)
    (6.60,0.989526)
    (6.70,0.990398)
    (6.80,0.991199)
    (6.90,0.991934)
    (7.00,0.992609)
    (7.10,0.993229)
    (7.20,0.993797)
    (7.30,0.994319)
    (7.40,0.994797)
    (7.50,0.995236)
    (7.60,0.995639)
    (7.70,0.996008)
    (7.80,0.996346)
    (7.90,0.996656)
    (8.00,0.996940)
    (8.10,0.997201)
    (8.20,0.997439)
    (8.30,0.997658)
    (8.40,0.997858)
    (8.50,0.998041)
    (8.60,0.998209)
    (8.70,0.998362)
    (8.80,0.998503)
    (8.90,0.998632)
    (9.00,0.998749)
    (9.10,0.998857)
    (9.20,0.998955)
    (9.30,0.999046)
    (9.40,0.999128)
    (9.50,0.999203)
    (9.60,0.999272)
    (9.70,0.999335)
    (9.80,0.999393)
    (9.90,0.999446)
    (10.00,0.999494)


     
    To compare these to the analog filter, we could derive the time equation and calculate the values up to 10 seconds, or we would use a circuit simulator. We want to plot anyway, so we use a circuit simulator for the analog and plot the values of the digital filter so we can compare. The following attachment shows the results of both the digital filter and the analog filter, and shows how remarkably similar the two came out to be.

    Vo1 is the analog filter output, Vo2 is the digital filter output, and the bottom plot is both of these superimposed on top of each other to show how nearly equal they came out to each other. We can barely tell there are two traces in that bottom plot.
     

    Attached Files:

    Last edited: Nov 22, 2011
    • Like Like x 1
  17. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Hi,

    After chatting with few people I came to know for testing or finding H(n) the impulse is tested with the help of speaker..........,how??
    I am not aware how they might do this?
     
  18. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    hypothetical impulse response illustration
     

    Attached Files:

    • 12.JPG
      12.JPG
      File size:
      114.5 KB
      Views:
      109
  19. MrAl

    MrAl Well-Known Member Most Helpful Member

    Joined:
    Sep 7, 2008
    Messages:
    11,026
    Likes:
    951
    Location:
    NJ
    Hi,

    You can approximate the impulse response to a system with a high level pulse as long as the system can take that high input. It only has to be able to take that high input for a very short time however, but we have to be aware that some components cant take a high voltage. Even a resistor has a maximum voltage rating associated with it so it might take some doing to get this to work without damaging anything.
    With a speaker i'd be afraid that there might be some winding arc over. Maybe they restrict their inputs to some lower level.

    The required pulse should have:
    Integral[t1 to t2](y(t)) dt=1

    In other words, the area under the pulse must be equal to 1, and the pulse should be as high as possible.
    So with a 100v pulse, we would need the pulse time to be 0.01 seconds. With a 1000v pulse, we would need the pulse time to be 0.001 seconds.

    If you want to investigate this a little better, you can choose some simulation network made with maybe one inductor and one capacitor and a resistor or two, then excite it with a high level pulse as explained above, then try to figure out what the impulse response is. You can compare your result in simulation to that of the true impulse response to see how the approximation correlates to the true response.
     
    Last edited: Nov 27, 2011
    • Like Like x 1
  20. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Then why we get diff. value of h(n) and how??
     
  21. MrAl

    MrAl Well-Known Member Most Helpful Member

    Joined:
    Sep 7, 2008
    Messages:
    11,026
    Likes:
    951
    Location:
    NJ
    I have no idea what you are asking again. Perhaps rephrase?
     

Share This Page