Steven_lord asked . 2021-12-24

DSP Question: invfreqs.m

I generate some coefficents for a filter and can inspect the frequency response as following:

 

%%Orginal Data
N = 5000;
data = cumsum(randn(N,1));
t = 252;
a = 2 / (t+1);
b = repmat(1-a,1 ,N).^(1:N); %b are your filter coeff 
b = b ./ sum(b); 
a = 1;

%%Plot the Filter on some example data
ma = filter(b, a, data);
figure;plot(data); hold all; plot(ma, 'r');  

%%Plot the Response
figure;freqz(b,1);          
[h,w] = freqz(b,1);
I now explain my problem. I am now in the situation where I have a frequency response (i.e. the vector "h") and know nothing else.
 
I would like to estimate from this my original "b" (the filter coefficents) to allow me to estimate my variable "t".
 
I thought I could use invfreqs.m (or invfreqz.m) to do this, but Im afraid I dont know how.
 
%%Find the impluse response
n = 10;  % I choose a large number allowing a good approximation
m = 0;  % I choose 0 here as I have 1 in my orignal filter ==> the output comes out as aNew = 1;
[bNew,aNew] = invfreqz(h,w,n,m);
%[bNew,aNew] = invfreqs(h,w,n,m);
sys = tf(bNew,aNew)

%%Plot the filter coeffcients
x1 = [0: 1/(size(b,2) -1)    : 1];
x2 = [0: 1/(size(bNew,2) -1) : 1];
figure;plot(x1,b); hold all; plot(x2,bNew, 'r');
When I inspect the final plot, I would expect to see the red line (bNew) as a good approximation to b. It is not. not even close.
 
Clearly I am doing something very wrong. Please could someone with experince of how this function works, explain my mistake.
 
many thanks!

invfreqs , matlab , programming

Expert Answer

John Michell answered . 2024-04-13 16:15:48

invfreqz.m has some odd rules re calling it. 

%%Orginal Data
  N = 5000;
  data = cumsum(randn(N,1));
  t = 252;
  a = 2 / (t+1);
  b = repmat(1-a,1 ,N).^(1:N); 
  b = b ./ sum(b); 
  a = 1;

%%Plot the Filter on some example data
ma = filter(b, a, data);
figure;plot(data); hold all; plot(ma, 'r');  

%%Plot the Response
figure;freqz(b,1, N);       
[h,w] = freqz(b,1,N);

%%Using dflit
%   b = 1; a = -1; %Sanity Check. simple difference filter
    % Hd = dfilt.df1([b a],1);   % num/ denom == a/b
    % fvtool(Hd);

%%Find the impluse response
% If n>(N-1) then you get a random answer! 
%If less then you get a visually different approximation,
%albiet it one with the same frequency response when plotted using freqz.m
n = N-1;  
m = 0;  % I choose 0 here as I have 1 in my orignal filter ==> the output comes out as aNew = 1;
wt = ones(size(w));

[bNew,aNew] = invfreqz(h,w,n,m);%, wt, 1000);  
%[bNew,aNew] = invfreqs(h,w,n,m);
%sys = tf(bNew,aNew);

%%Plot the filter coeffcients
% These now match if you used n = N-1
x1 = [0: 1/(size(b,2) -1)    : 1];
x2 = [0: 1/(size(bNew,2) -1) : 1];
figure;plot(x1,b); hold all; plot(x2,bNew, 'r');

%these always match
figure; freqz(b);
figure; freqz(bNew);
%invfreqz expects the complex-valued frequency response, not just the
%magnitude. If you only have the magnitude then use fdesign.arbmag
% http://www.mathworks.co.uk/products/dsp-system/demos.html?file=/products/demos/shipping/dsp/arbmagdemo.html
M = 1000; 
F = w/max(w); 
A = abs(h); 
d = fdesign.arbmag('N,F,A',M,F,A); 
Hd = design(d,'freqsamp'); 
%fvtool(Hd,'MagnitudeDisplay','Zero-phase');

b3 = Hd.Numerator(501:end); %symmetric so just take half. 
x1 = [0: 1/(size(b,2) -1) : 1]; 
x3 = [0: 1/(size(b3,2) -1) : 1]; 
figure;plot(x1,b); hold all; plot(x2,bNew, 'r'); plot(x3,b3, 'g');

 


Not satisfied with the answer ?? ASK NOW

Frequently Asked Questions

MATLAB offers tools for real-time AI applications, including Simulink for modeling and simulation. It can be used for developing algorithms and control systems for autonomous vehicles, robots, and other real-time AI systems.

MATLAB Online™ provides access to MATLAB® from your web browser. With MATLAB Online, your files are stored on MATLAB Drive™ and are available wherever you go. MATLAB Drive Connector synchronizes your files between your computers and MATLAB Online, providing offline access and eliminating the need to manually upload or download files. You can also run your files from the convenience of your smartphone or tablet by connecting to MathWorks® Cloud through the MATLAB Mobile™ app.

Yes, MATLAB provides tools and frameworks for deep learning, including the Deep Learning Toolbox. You can use MATLAB for tasks like building and training neural networks, image classification, and natural language processing.

MATLAB and Python are both popular choices for AI development. MATLAB is known for its ease of use in mathematical computations and its extensive toolbox for AI and machine learning. Python, on the other hand, has a vast ecosystem of libraries like TensorFlow and PyTorch. The choice depends on your preferences and project requirements.

You can find support, discussion forums, and a community of MATLAB users on the MATLAB website, Matlansolutions forums, and other AI-related online communities. Remember that MATLAB's capabilities in AI and machine learning continue to evolve, so staying updated with the latest features and resources is essential for effective AI development using MATLAB.

Without any hesitation the answer to this question is NO. The service we offer is 100% legal, legitimate and won't make you a cheater. Read and discover exactly what an essay writing service is and how when used correctly, is a valuable teaching aid and no more akin to cheating than a tutor's 'model essay' or the many published essay guides available from your local book shop. You should use the work as a reference and should not hand over the exact copy of it.

Matlabsolutions.com provides guaranteed satisfaction with a commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been empanelled after extensive research and quality check.

Matlabsolutions.com provides undivided attention to each Matlab assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work done at the best price in industry.