Question: Python programming assignment While writing a mathematical tool for signal processing, we find ourselves needing to work with sums of cosine functions defined as: f(1)

Python programming assignment

Python programming assignment While writing a mathematical tool for signal processing, wefind ourselves needing to work with sums of cosine functions defined as:

While writing a mathematical tool for signal processing, we find ourselves needing to work with sums of cosine functions defined as: f(1) = cos(26 x ix ) cos(2x xi XI) N where ci are the coefficients for cosine functions of increasing frequency. The higher frequency (as i becomes large) terms tend towards zero, so we limit ourselves to a finite sum, only storing the N first coefficients of c, treating all higher frequency terms as zero. Part A Write a class CosineSerie that represents a finite sum of cosine functions as described above. The constructor should take a list of the first N coefficients of the series (neglecting higher frequency terms). Add repr support so that it looks similar to the example below. Part B Add support for iter and len which traverses and gives the mumber of and the coefficients respectively. Part C Add a method evaluate_term which should take an index i and a value x and return the i-th term of the sum: COS(2 x ixx) Negative index values should raise an IndexError, but higher frequency terms should return zero (see example). Part D Make your CosineSerie callable so that we can evaluate the entire sum: f = CosineSerie [3., 2., 1.]) print (f(0.5)) # should equal: 3*cos(2*pi*0*0.5) + 2*cos (2*pi*1*0.5) + 1*cos (2*1*2*0.5) = 2.0 Part E We can also add two series together by simply adding their coefficients; cos(27 x i x =)) + d.cos(24 X i X I)) = c+d) cos(2x xi XI) (25 Xix =)) + Introduce a private static method __add_coefficients that takes 2 lists and adds them together by padding out the shorter list with zeros (if necessary). E.g. [1, 2, 3] and [1, 1, 1, 1) should produce [2, 3, 4, 1]. Use this method to implement support for + and += in CosineSerie. 0 Test code a = CosineSerie([1., 2., 3.]) b = CosineSerie([4., 6.]) print("a(0.5) -", a(0.5)) print("repr (b) =", repr (b)) c = CosineSerie([2., 2.]) C + a d = b + CosineSerie([1., 1., 1., 1.]) print("c.evaluate_term(0, 0.5) -", c.evaluate_term(0, 0.5)) print("c.evaluate_term(1, 0.5) =", c.evaluate term(1, 0.5)) print("c.evaluate_term(2, 0.5) =", c.evaluate_term (2, 0.5)) assert c. evaluate_term(3, 0.5) == 0 assert c. evaluate_term(4, 0.5) == 0 try: c.evaluate_term(-1, 0.5) except IndexError as e: print("IndexError:', e) print(f"d has {len(a)} non-zero coefficients") print("Coefficients of d:", end=' ) for coefficient in d: print(coefficient, end='') print() Output a(0.5) = 2.0 repr(b) = CosineSerie([4.0, 6.0]) c. evaluate_term(0, 0.5) = 3.0 c.evaluate_term(1, 0.5) = -4.0 c. evaluate_term(2, 0.5) = 3.0 IndexError: Negative index d has 4 non-zero coefficients Coefficients of d: 5.0 7.0 1.0 1.0 While writing a mathematical tool for signal processing, we find ourselves needing to work with sums of cosine functions defined as: f(1) = cos(26 x ix ) cos(2x xi XI) N where ci are the coefficients for cosine functions of increasing frequency. The higher frequency (as i becomes large) terms tend towards zero, so we limit ourselves to a finite sum, only storing the N first coefficients of c, treating all higher frequency terms as zero. Part A Write a class CosineSerie that represents a finite sum of cosine functions as described above. The constructor should take a list of the first N coefficients of the series (neglecting higher frequency terms). Add repr support so that it looks similar to the example below. Part B Add support for iter and len which traverses and gives the mumber of and the coefficients respectively. Part C Add a method evaluate_term which should take an index i and a value x and return the i-th term of the sum: COS(2 x ixx) Negative index values should raise an IndexError, but higher frequency terms should return zero (see example). Part D Make your CosineSerie callable so that we can evaluate the entire sum: f = CosineSerie [3., 2., 1.]) print (f(0.5)) # should equal: 3*cos(2*pi*0*0.5) + 2*cos (2*pi*1*0.5) + 1*cos (2*1*2*0.5) = 2.0 Part E We can also add two series together by simply adding their coefficients; cos(27 x i x =)) + d.cos(24 X i X I)) = c+d) cos(2x xi XI) (25 Xix =)) + Introduce a private static method __add_coefficients that takes 2 lists and adds them together by padding out the shorter list with zeros (if necessary). E.g. [1, 2, 3] and [1, 1, 1, 1) should produce [2, 3, 4, 1]. Use this method to implement support for + and += in CosineSerie. 0 Test code a = CosineSerie([1., 2., 3.]) b = CosineSerie([4., 6.]) print("a(0.5) -", a(0.5)) print("repr (b) =", repr (b)) c = CosineSerie([2., 2.]) C + a d = b + CosineSerie([1., 1., 1., 1.]) print("c.evaluate_term(0, 0.5) -", c.evaluate_term(0, 0.5)) print("c.evaluate_term(1, 0.5) =", c.evaluate term(1, 0.5)) print("c.evaluate_term(2, 0.5) =", c.evaluate_term (2, 0.5)) assert c. evaluate_term(3, 0.5) == 0 assert c. evaluate_term(4, 0.5) == 0 try: c.evaluate_term(-1, 0.5) except IndexError as e: print("IndexError:', e) print(f"d has {len(a)} non-zero coefficients") print("Coefficients of d:", end=' ) for coefficient in d: print(coefficient, end='') print() Output a(0.5) = 2.0 repr(b) = CosineSerie([4.0, 6.0]) c. evaluate_term(0, 0.5) = 3.0 c.evaluate_term(1, 0.5) = -4.0 c. evaluate_term(2, 0.5) = 3.0 IndexError: Negative index d has 4 non-zero coefficients Coefficients of d: 5.0 7.0 1.0 1.0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!