Hi need help for overriding system objects in MATLAB

4 views (last 30 days)
Hi this is my topic in details The thesis focuses on designing and analyzing polynomial sampling rate converters using interpolation techniques. While MATLAB currently supports Lagrange interpolation, the research aims to explore the use of spline interpolation for sampling rate conversion. A MATLAB class will be developed for designing converters based on spline interpolation, and the efficiency of these converters will also be analyzed.
The dsp.FarrowRateConverter class in MATLAB allows for the creation of a polynomial sampling rate converter with user-defined properties, including direct specification of polynomial coefficients, such as for Lagrange or spline interpolation. However, the calculation of spline coefficients must be done externally, as the class does not handle this. The class offers built-in methods for analyzing and comparing converter objects.
The dsp.FarrowRateConverter class can be extended to include a method for calculating spline coefficients within the class.
I have overridden dsp.FarrowRateConverter
classdef SplineFarrowRateConverter < dsp.FarrowRateConverter
% SplineFarrowRateConverter Spline-based sample rate converter with arbitrary conversion factor.
% This class extends dsp.FarrowRateConverter to use Spline interpolation
% instead of Lagrange interpolation.
methods
function obj = SplineFarrowRateConverter(varargin)
% Constructor: Inherit properties from the base class
obj@dsp.FarrowRateConverter(varargin{:});
% Check if PolynomialOrder is specified and validate it
if strcmp(obj.Specification, 'Polynomial order')
validateattributes(obj.PolynomialOrder, ...
{'numeric'}, ...
{'real', 'integer', 'positive', 'scalar', 'finite'}, ...
'SplineFarrowRateConverter', 'PolynomialOrder');
end
end
function setPolynomialOrder(obj, N)
% Method to set the PolynomialOrder with validation
validateattributes(N, ...
{'numeric'}, ...
{'real', 'integer', 'positive', 'scalar', 'finite'}, ...
'setPolynomialOrder', 'PolynomialOrder');
obj.PolynomialOrder = N;
needToDesignFilter(obj); % Reset filter design to accommodate new order
end
end
methods (Access = protected)
function setFarrowSRCProperties(obj)
% Override the base method to calculate Spline coefficients
if obj.CoefficientsExist
return;
end
isPolynomialOrderSpec = strcmp(obj.Specification, 'Polynomial order');
if isPolynomialOrderSpec
NpOrCf = obj.PolynomialOrder;
else
NpOrCf = obj.Coefficients;
end
coder.extrinsic('dsp.FarrowRateConverter.deriveCoefficients');
if isempty(coder.target)
% Calculate Spline coefficients instead of Lagrange
[L, M, C] = obj.deriveSplineCoefficients(obj.InputSampleRate, ...
obj.OutputSampleRate, obj.OutputRateTolerance, isPolynomialOrderSpec, NpOrCf);
obj.CoefficientsExist = true;
% Update the metadata (fdesign and fmethod)
if isPolynomialOrderSpec
fd = fdesign.polysrc(L, M, 'Fractional Delay', 'Np', NpOrCf, obj.InputSampleRate * L);
if ~isempty(getcurrentspecs(fd))
fmd = fdfmethod.lagrangesrc; % Could be replaced with the equivalent for Spline
else
fmd = [];
end
setMetaData(obj, fd, fmd);
end
else
[L, M, C] = coder.const(@obj.deriveSplineCoefficients, obj.InputSampleRate, ...
obj.OutputSampleRate, obj.OutputRateTolerance, isPolynomialOrderSpec, NpOrCf);
end
obj.InterpolationFactor = L;
obj.DecimationFactor = M;
obj.PolynomialCoefficients = C;
end
function [L, M, C] = deriveSplineCoefficients(obj, FsIn, FsOut, FsTol, isPolynomialOrderSpec, NpOrCf)
% Derive coefficients specific to Spline interpolation
[L, M] = dsp.SampleRateConverter.myrat(FsIn, FsOut, FsTol);
if isPolynomialOrderSpec
% Generate Spline coefficients based on the specified order
C = obj.designSplineCoefficients(NpOrCf);
else
C = (NpOrCf).';
end
end
function C = designSplineCoefficients(~, N)
% Calculate the coefficients for Spline interpolation using B-Spline coefficients
L = N; % Degree of the B-Spline is typically set to N (the polynomial order)
C = computeBSplineCoefficients(L, N); % Call the new function to compute B-Spline coefficients
end
end
end
function a = computeBSplineCoefficients(L, N)
% L: Degree of the B-Spline
% N: Number of control points - 1
% a: Vector containing the B-Spline coefficients
% Initialize the coefficients vector
a = zeros(1, N + 1);
% Define the knot vector t_i = i for i = 0 to N
t = 0:N;
% Loop over each coefficient to calculate a_i
for i = 0:N
prod = 1; % Initialize the product
for k = 0:N
if k ~= i
prod = prod * (t(i+1) - t(k+1)); % MATLAB uses 1-based indexing
end
end
% Calculate the coefficient a_i
a(i+1) = (-1)^(L+1) / prod; % a(i+1) because MATLAB is 1-based
end
end
This is generating error >> SplineFRC3
The class dsp.SampleRateConverter has no Constant property or Static method named 'myrat'.
Error in SplineFarrowRateConverter/deriveSplineCoefficients (line 74)
[L, M] = dsp.SampleRateConverter.myrat(FsIn, FsOut, FsTol);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in SplineFarrowRateConverter/setFarrowSRCProperties (line 48)
[L, M, C] = obj.deriveSplineCoefficients(obj.InputSampleRate, ...
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in dsp.FarrowRateConverter/validateInputsImpl (line 608)
setFarrowSRCProperties(obj);
^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in SplineFRC3 (line 12)
response = src(impulse); % Use the object directly to invoke the processing
^^^^^^^^^^^^
This error is occuring when I try to observe its frequency response % Create an instance of your SplineFarrowRateConverter
src = SplineFarrowRateConverter('InputSampleRate', 44100, ...
'OutputSampleRate', 48000, ...
'Specification', 'Polynomial order', ...
'PolynomialOrder', 3); % Adjust the order here
% Generate an impulse signal to analyze the filter response
impulse = [1; zeros(1023, 1)]; % Create an impulse signal
% Process the impulse through the rate converter
response = src(impulse); % Use the object directly to invoke the processing
% Create a digital filter object from the impulse response
% Here, we use dsp.FIRFilter as a representative filter type for visualization
firFilter = dsp.FIRFilter('Numerator', response');
% Visualize the filter's frequency response using fvtool
fvtool(firFilter); Please could you correct the error and also I want to access the computation complexity of this code ,the number of adders and multipliers and also value of coeffiecients and frequency response for polynomial orders more than 3
  1 Comment
Rohitashya
Rohitashya on 24 Sep 2024
The functions of Bspline are given in the function
% Function to perform spline interpolation
function y_interp = spline_interp(t, L, knots, x_samples)
m = 1;
N = length(x_samples);
y_interp = 0;
for k = m:m+L
beta = normalized_b_spline(t, L, knots, k);
y_interp = y_interp + beta * x_samples(k);
end
end
% File: spline_interpolation.m
% Function to compute the truncated power function phi_i(t)
function phi = truncated_power(t, ti, L)
if t < ti
phi = 0;
else
phi = (t - ti)^L;
end
end
% Function to compute the normalized L-th order B-spline beta_L_m(t)
function beta = normalized_b_spline(t, L, knots, m)
B_L_m = b_spline(t, L, knots, m);
beta = (knots(end) - knots(1)) * B_L_m;
end
% Function to compute the L-th order B-spline B_L_m(t)
function B = b_spline(t, L, knots, m)
n = length(knots);
if L == 0
if knots(m) <= t && t < knots(m+1)
B = 1;
else
B = 0;
end
else
left_term = (t - knots(m)) / (knots(m+L) - knots(m)) * b_spline(t, L-1, knots, m);
right_term = (knots(m+L+1) - t) / (knots(m+L+1) - knots(m+1)) * b_spline(t, L-1, knots, m+1);
B = left_term + right_term;
end
end

Sign in to comment.

Answers (1)

Divyanshu
Divyanshu on 25 Sep 2024
Edited: Divyanshu on 25 Sep 2024
The error 'no constant property or static method named 'func' ' is generally thrown when we try to access some method using 'Dot Indexing' on a class object but the method is not a member of the class.
In your case as well you are using system object 'dsp.SampleRateConverter' but in documentation dsp.SampleRateConverter there is no method 'myRat' for this object.
Therefore you might need to override the class 'dsp.SampleRateConverter' as well and define method 'myRat'.
Additionally, for your second query regarding calculating computation complexity of the code, you can refer the following MATLAB Answer threads:
Hope it helps!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!