# Untitled

a guest
Aug 3rd, 2012
20
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. function lagrangeResamplingDemo()
2.     originDefinition = 0; % see comment for LagrangeBasisPolynomial() below
3.
4.     % Regarding order: From [1]
5.     % "Indeed, it is easy to show that whenever Q is odd, none of the
6.     % impulse responses corresponding to Lagrange interpolation can have
7.     % linear phase"
8.     % Here, order = Q+1 => odd orders are preferable
9.     order = 5;
10.
11.     % *******************************************************************
12.     % Set up signals
13.     % *******************************************************************
14.     nIn = order + 1;
15.     nOut = 80;
16.
17.     % Reference data: from [1] fig. 8, linear-phase type
18.     % ref = [-0.032, -0.056, -0.064, -0.048, 0, ...
19.     %       0.216, 0.448, 0.672, 0.864, 1, ...
20.     %       0.864, 0.672, 0.448, 0.216, 0, ...
21.     %       -0.048, -0.064, -0.056, -0.032, 0];
22.     % tRef = (1:size(ref, 2)) / size(ref, 2);
23.     tRef = [0:1/10:1-1/10];
24.     ref = sin( tRef*2*pi );
25.
26.     % impulse input to obtain impulse response
27.     %inData = zeros(1, nIn);
28.     %inData(1) = 1;
29.     inData = sin( [0:1/6:1-1/6] *2*pi );
30.     nIn = length(inData);
31.
32.     % Get Lagrange polynomial coefficients
33.     c0 = lagrangeBasisPolynomial(order, 0, originDefinition);
34.     c1 = lagrangeBasisPolynomial(order, 1, originDefinition);
35.     c2 = lagrangeBasisPolynomial(order, 2, originDefinition);
36.     c3 = lagrangeBasisPolynomial(order, 3, originDefinition);
37.     c4 = lagrangeBasisPolynomial(order, 4, originDefinition);
38.     c5 = lagrangeBasisPolynomial(order, 5, originDefinition);
39.
40.     % *******************************************************************
41.     % Plot
42.     % *******************************************************************
43.     figure(2), clf, hold on
44.     stem(tRef, ref, 'r');
45.
46.     % *******************************************************************
47.     % Apply Lagrange interpolation to input data
48.     % *******************************************************************
49.     for i=0:nOut-1
50.
51.         % outTimeAtInputInteger
52.         temp = floor( (i/nOut)*nIn );
53.
54.         % the value of the input sample that appears at FIR tap 'ixTap'
55.         d0 = inData( mod(temp + 0 - order, nIn) + 1 );
56.         d1 = inData( mod(temp + 1 - order, nIn) + 1 );
57.         d2 = inData( mod(temp + 2 - order, nIn) + 1 );
58.         d3 = inData( mod(temp + 3 - order, nIn) + 1 );
59.         d4 = inData( mod(temp + 4 - order, nIn) + 1 );
60.         d5 = inData( mod(temp + 5 - order, nIn) + 1 );
61.
62.         % Evaluate the Lagrange polynomials, resulting in the time-varying FIR tap weight
63.         evalFracTime = -0.5 + mod(i,nOut/nIn) * (nIn/nOut);
64.
65.         cTap0 = polyval(c0, evalFracTime);
66.         cTap1 = polyval(c1, evalFracTime);
67.         cTap2 = polyval(c2, evalFracTime);
68.         cTap3 = polyval(c3, evalFracTime);
69.         cTap4 = polyval(c4, evalFracTime);
70.         cTap5 = polyval(c5, evalFracTime);
71.
72.         % FIR operation: multiply FIR tap weight with input sample
73.         outData(i+1) = d0*cTap0 + d1*cTap1 + d2*cTap2 + d3*cTap3 + d4*cTap4 + d5*cTap5;
74.
75.         plot( i/nOut, outData(i+1), 'b.');
76.         pause(0.01)
77.     end
78.
79.     legend('impulse response', 'reference result');
80.     title('Lagrange interpolation, impulse response');
81.
82. end
RAW Paste Data