I am trying to make a plot the impulse response of both an IIR and FIR system by using Matlab's filter command and no other pre-existing Matlab functions. The filter must be able to handle a sample input such as plot([1 2 2], [0 1 .8]). The problem looks like this:

```
function iplot(b, a)
% IPLOT Impulse response of system.
% IPLOT(B,A,N) is the N-point impulse response
% of the filter B/A:
% -1 -nb
%
% B(z) b(1) + b(2)z + .... + b(nb+1)z
%
% H(z) = ---- = ---------------------------------
%
% -1 -n
% A(z) a(1) + a(2)z + .... + a(na+1)z
% If h[n] FIR, then N = length(h);
% given numerator and denominator coefficients in vectors B and A.
% N is specified according to the following rule:
% If If h[n] is IIR and increasing (i.e. |h[n]|-->inf as n-->inf),
% then N=20;
% h[n] is IIR and decreasing (i.e. |h[n]|-->0 as n-->inf),
% then the maximum N is determined such that
% rms value of h(1:N) = 0.999 * rms value of h(1:1000).
% However, in this case, N must also be chosen such that 10 <=
% N <= 100
```

This is what I have so far. I know its not correct and right now it doesn't plot anything but its as far as I've gotten. Any Help is much appreciated!

```
h = filter(a,b,[1,zeros(1,999)]);
N = length(h);
plot(h, zeros(length(h),999), 'b');
poles = roots(a);
if poles <= 1
N = 20;
plot(h, N, 'b');
end
end
```

Answer:

The first line of code is correct. However, you need to **reverse** the order of `b`

and `a`

when calling `filter`

. `b`

stands for the numerator coefficients while `a`

stands for the denominator coefficients. That's in your docstring too.

However, you are correctly specifying the input as being a single input. However, your plotting code isn't correct. How `plot`

works is that it takes in a set of `x`

values and a set of `y`

values and we plot those points as pairs. Because you are plotting a discrete output signal, I would recommend you use `stem`

instead of `plot`

. This way, each point gets drawn with a line that goes from the horizontal axis up to the point of interest.

In any case, you need to provide a set of `x`

values. In this case, you want a vector that goes from 0 up to 999. The `y`

values is the output impulse response due to `filter`

. Currently, your `x`

values are the output impulse response and your `y`

values are a bunch of zeroes. This will be visualized as a whole bunch of points being clustered at `y = 0`

which is probably not what you want.

As such, you simply need to do this:

```
h = filter(b,a,[1,zeros(1,999)]);
plot(0:999, h, 'b'); %// Change
```

With regards to my recommendation, I would use `stem`

instead:

```
stem(0:999, h, 'b');
```

Also, the last part of your code is plotting the poles of your filter. You should also make a **separate** figure and plot those results instead. You're not doing this, so what happens is that when you try and plot the poles, the figure that contains your impulse response **will be overwritten**. Calling `plot`

multiple times with the current window open will overwrite the plot with the last invocation to `plot`

. Therefore, spawn a new `figure`

window before proceeding:

```
figure; %// New window
h = filter(b,a,[1,zeros(1,999)]);
stem(0:999, h, 'b'); %// Change
figure; %// New window
poles = roots(a); %// Find roots
%// Plot the roots as single dots
plot(real(poles), imag(poles), 'b.');
```

However, this doesn't answer your problem statement. The problem statement says:

If

`h[n]`

is FIR, then`N = length(h)`

; given numerator and denominator coefficients in vectors`B`

and`A`

.

`N`

is specified according to the following rule: If`h[n]`

is IIR and increasing (i.e.`|h[n]|-->inf as n-->inf`

), then`N=20`

;

`h[n]`

is IIR and decreasing (i.e.`|h[n]|-->0 as n-->inf`

), then the maximum N is determined such that the`rms`

value of`h(1:N) = 0.999 * rms value of h(1:1000)`

. However, in this case,`N`

must also be chosen such that`10 <= N <= 100`

We know that a FIR filter is actually a FIR filter if you specify `A`

to be 1 or just a single value. So when you detect that the length of `A`

is 1, then `N = length(h);`

. However, if `a`

is **not** just a single value, we have to check for two cases. The first case is when the impulse response is increasing. You can check that by using `diff`

and making sure that all neighbouring differences of your impulse response are positive. The next case is when `h`

is decreasing. You can check that by making sure that all the neighbouring differences of your impulse response are negative.

If it's the increasing case, then choose `N = 20`

. If it's the decreasing case, then choose the `N`

value to be what you saw above, and you can use the `rms`

function in MATLAB to help you do that. We also need to make sure that this value is between 10 and 100, and we'll also want to **round** the value as we are trying to choose an integer number of points and `rms`

will probably give you a floating point number.

Therefore, perform `filter`

with the default 1000 points, then you'll need to **truncate** the response depending on what the inputs are into the function. First we need to check for FIR, then the IIR cases to decide what values of `N`

to display.

Therefore:

```
figure; %// New window
h = filter(b,a,[1,zeros(1,999)]);
%// Decide on the value of N
if length(a) == 1
N = length(h);
else
d = diff(h);
if all(d >= 0) %// Check for increasing
N = 20;
else %// Decreasing case
%// Find RMS value
N = round(0.999*rms(h));
%// Ensure that 10 <= N <= 100
if N < 10
N = 10;
end
if N > 100
N = 100;
end
end
end
%// Plot just up to the first N values
stem(0:N-1, h(1:N), 'b');
figure; %// New window
poles = roots(a); %// Find roots
%// Plot the roots as single dots
plot(real(poles), imag(poles), 'b.');
```

matlab

What is the ellipsis for in this Matlab statement? frame = insertObjectAnnotation(frame, 'rectangle', ... bboxes, labels); ...I could not find in their online doc....

matlab,plot

I have multiple 2D line plots in Matlab (they represent some wave moving through space). Each plot represents the wave at some time t. I want to animate through these plots (i.e. show the first plot for a fraction of a second, then show the next one, and the next,...

matlab,svm,auc

I'm having a weird problem in training an SVM with an RBF kernel in Matlab. The issue is that, when doing a grid search, using 10-fold cross-validation, for the C and Sigma values I always get AUC values equal to approximately .50 (varying between .48 and .54 depending) -- I...

javascript,jquery,plot,flot

I am working on an application where I am trying to plot a similar graph to what is plotted at Open Weather. I am using the FlotJS library to query their API and plot the graphs. Here is my code. I apologize for the verbosity. /* * RUN PAGE GRAPHS...

matlab,matlab-figure

I'm struggling to plot multiple functions on one figure. Here is the code that I have: syms t a; a=0.9514; F1=0.5*sqrt(3*t^2); F2=-0.28375*t^2+1.155*a*(t-a)+1; F3=1; E1=diff(F1,t); E2=diff(F2,t); E3=diff(F3,t); I want to plot E1, E2 and E3, each only within a certain range, to make a "composite" line. I've tried plotting with ezplot...

matlab,polynomial-math

I have the following code: syms z Gc=1.582*(1-0.3679*z^-1)/(1+.418*z^-1); Ghp=.3679*(z^-1)*(1+.718*z^-1)/((1-z^-1)*(1-.3679*z^-1)); T=(Gc*Ghp)/(1+Gc*Ghp); clipboard('copy', latex(simplifyFraction(T))); Which results in following for T: How can I normalise coefficients? I.e. I want the z2 in denominator and z in numerator to have the coefficient of 1. Is there any function in Matlab to do so?...

matlab,filtering,convolution

I'm trying to design a Wiener filter in Matlab for a deconvolution problem but I'm having a lot of problems. I have a gaussian white noise process with a variance of 1.2 and a impulse response which has length two. Its values are g(0) = 5 and g(1) = 4....

matlab,matrix,least-squares

How can I solve , where and and in the least squares sense in matlab? So I'd like to have the minimizing as output....

python,matplotlib,plot,fill

I'm trying to get access to the shaded region of a matplotlib plot, so that I can remove it without doing plt.cla() [since cla() clears the whole axis including axis label too] If I were plotting I line, I could do: import matplotlib.pyplot as plt ax = plt.gca() ax.plot(x,y) ax.set_xlabel('My...

matlab

my matlab version is 2012a. when I use fitcknn,has this error: Undefined function 'fitcknn' for input arguments of type 'cell'. how to calculate probability for each class for predicate with knn without fitcknn? after use this code, I want to calculate prob_estimates for each neighbors: knn = ClassificationKNN.fit(trainsample, trainlabel,'NumNeighbors',7); y...

matlab,simulink

I am working on a simple System Object in Matlab/Simulink. It looks like this : classdef realtime_header_detectorSO < matlab.System & matlab.system.mixin.Propagates % correlateHeader % % This template includes the minimum set of functions required % to define a System object with discrete state. properties Header %nrOfBitsInPreviousStep=0; s=100; d=zeros(1,s); end properties...

matlab,system,equation

sorry if it already has a answer..i tried other links but it didn't understand i have 2 1*63 array .landa and v. each of their members are syms. and each v member is a function of all landa members.i have already calculated v members and they are all symbolic equations...

matlab,image-processing

I have a labeled matrix containing two objects. How can I thicken an object to a curve? Actually I have the following image: and I want this: Each pixel of the resulting curve is the median of each column. But if you have another idea, it is acceptable, because I...

matlab,machine-learning

This is my implementation of CostFunctionJ: function J = CostFunctionJ(X,y,theta) m = size(X,1); predictions = X*theta; sqrErrors =(predictions - y).^2; J = 1/(2*m)* sum(sqrErrors); But when I try to enter the command in MATLAB as: >> X = [1 1; 1 2; 1 3]; >> y = [1; 2; 3];...

string,matlab,filenames

I am using the below mentioned code to get the file names of images according to their id's from images_1 text file as strings and use them to read the images from their directory image_count=1; for image_count=1:6 file=fopen('D:\Academics\New folder\CUB_200_2011\images_1.txt','r'); C = textscan(file, '%s'); original_image=imread('D:\Academics\New folder\CUB_200_2011\images\%s','C{1}{2*(image_count)}'); imshow(original_image) end I am able...

matlab

So, I want to be able to look at (read: copy) MATLAB's NonLinearModel method of printing the regression results to the screen such as this. Nonlinear regression model: y ~ (alpha1 - alpha2*t^0.5) Estimated Coefficients: Estimate SE tStat pValue alpha1 1.0253 0.0082253 124.66 4.8823e-24 alpha2 0.0061783 0.00073277 8.4314 4.4834e-07 Number...

python,numpy,matplotlib,plot

I have two arrays. One is the raw signal of length (1000, ) and the other one is the smooth signal of length (100,). I want to visually represent how the smooth signal represents the raw signal. Since these arrays are of different length, I am not able to plot...

r,plot,ggplot2,cdf

Currently I am doing some cumulative distribution plot using R and I tried to set x-axis with decreasing power values (such as 10000,1000,100,10,1) in equal sizes but I failed: n<-ceiling(max(test)) qplot(1:n, ecdf(test)(1:n), geom="point",xlab="check-ins", ylab="Pr(X>=x)")+ geom_step() +scale_x_reverse(breaks=c(10000,1000,100,10,1)) +scale_shape_manual(values=c(15,19)) It seems that the output has large interval for 10000, then all the...

image,matlab,image-processing,mask,boundary

I'm trying to create a mask (or similar result) in order to erase pieces of a binary image that are not attached to the object surrounded by the boundary. I saw this thread (http://www.mathworks.com/matlabcentral/answers/120579-converting-boundary-to-mask) to do this from bwboundaries, but I'm having trouble making suitable changes to it. My goal...

c++,matlab

Is there an alternative way to call a C++ class using MATLAB, and operate its methods on MATLAB variables?

plot,shiny,reactive-programming,weight

I am filtering a tbale and a heat-map simultaneously. During filtering, the width of the table is changing such that width of particular columns remains the same. Width of the heat-map remains the same during filtering. I want the heat-map to change its weight in the same way as the...

python,sql,matplotlib,plot

from sqlalchemy import create_engine import _mssql from matplotlib import pyplot as plt engine = create_engine('mssql+pymssql://**:****@127.0.0.1:1433/AffectV_Test') connection = engine.connect() result = connection.execute('SELECT Campaign_id, SUM(Count) AS Total_Count FROM Impressions GROUP BY Campaign_id') for row in result: print row connection.close() The above code generates an array: (54ca686d0189607081dbda85', 4174469) (551c21150189601fb08b6b64', 182) (552391ee0189601fb08b6b73', 237304) (5469f3ec0189606b1b25bcc0',...

python,csv,matplotlib,graph,plot

I am trying to plot a graph with colored markers before and after threshold value. If I am using for loop for reading the parsing the input file with time H:M I can plot and color only two points. But for all the points I cannot plot. Input akdj 12:00...

javascript,google-maps,plot

I am writing a simple application to plot multiple marker in the google map and connect it thought a path as it looks like a route . I can plot multiple marker . and search some article which plot the route map . but i can not plot it ....

matlab

I have 20 column vectors in the workspace and I'd like them all to be contained within a cell array. They're all labelled data1, data2, data3,...data20. Is there a faster way to do this than: cell = {data1, data2, data3, data4, data5, data6, ...} all the way up to data20?...

plot,gnuplot,highlight

I have 4 points I would like to plot using gnuplot, but with lines. The problem is, I can't find how to highlight these 4 points on the drawn line. I would like for the plot to be a line through the points, but that these points are also clearly...

matlab,plot

I have a semilogarithmic plot which works so far with semilogx. Now I would like to change the tick labels. Now it says 10^8 10^9 ... 10^13, but I would like to see 1e8, 1e9, ... 1e13 on the x-axis. How can I change that? Cheers Manuel...

matlab,plot

I want to display three figures in a figure window. Assuming that I divide 2x2 regions. subplot(2,2,1) ---------+----------- | R1 | R2 | ---------+----------- | R3 | R4 | ---------+----------- I want to show a figure merging R1 and R3 ant other two in R2 and R4 I can display...

matlab,image-processing

I am trying to read all the images in the folder in MATLAB using this code flst=dir(str_Expfold); But it shows me output like this. which is not the sequence as i want. Can anyone please tell me how can i read all of them in sequence? for giving downmark, please...

matlab

Is it possible to add statements in between the codes. For example: If I have a code like this, r(:,1) = a(:,1) - a(:,2); Then can I write it as, r(:,1) = a(:,1)("this is a constant") - a(:,2)("this is a variable"); ...

matlab,plot,legend,movie

i have a question regarding legend for movies. This is my code: fig = figure(); for i = 1: 70000 plot(signal1) hold on; plot([i,i],[-5,5]) plot(signal2,'r') hold off; title('\fontsize{14} my data'); legend('signal1','signal2'); axis tight; f(i) = getframe(fig); end The legend shows the same colors for the first two things I plot....

matlab,time-frequency

Code tfr = abs ( tfr ); [row_size, column_size] = size(tfr); tfr = tfr(1:round(row_size/2), 1:row_size); surf(tfr); view(2); I get in R2014b of OSX 10.10.3 Yosemite but rotating around shows that the cells should not be black Why is the output black? I wonder if this is a hardware problem or...

python,matplotlib,plot,google-visualization,heatmap

I am trying to plot a heatmap on top of an image. What I did: import matplotlib.pyplot as plt import numpy as np import numpy.random import urllib #downloading an example image urllib.urlretrieve("http://tekeye.biz/wp-content/uploads/2013/01/small_playing_cards.png", "/tmp/cards.png") #reading and plotting the image im = plt.imread('/tmp/cards.png') implot = plt.imshow(im) #generating random data for the histogram...

arrays,matlab

I have two vectors, one of them stores the width dimension of a set of images and another one the height of these set of images. I want to use these values as two dimensional vectors [width height] and store them in a matrix. The first line, for instance, keeps...

matlab,image-processing

I have a binary matrix containing several binary objects and I want to bridge between them. Actually I have the following picture: And the result has to be like this: Is there any function or a shortcut way, other than loops, for this problem?...

matlab,distribution,sampling,random-sample

How can I create a vector x in Matlab that has values between 0.8 and 1.2, randomly sampled from a: 1. Uniform 2. Normal distribution? There are a lot of functions dealing with distributions, but I'm having trouble using them properly....

matlab,typeconverter

I'm currently using a MATLAB to work and I need some help: I need to convert my output data (variable: units) be a double instead of a cell because I must perform a sum: units = inputdlg(question,title); sum = units + i; I've tried this code also but didn't solve...

matlab,matrix

I have a matrix looks like: 0 0 0 0 0 1 0 0 0 0 0 2 0 0 0 0 0 2 0 0 0 0 0 1 0 1 0 0 0 1 0 4 0 0 0 0 0 3 0 0 6 0 0 4...

matlab,matrix,multidimensional-array,scalar

I'm having problems multiplying specific values within my multidimensional matrix by a scalar. My matrix has the following dimension: size(comDatabe) = 5 10 3 397 10 The third dimension is an x-y-z coordinate frame. Something went wrong and now my y-axis is defined upside down for one subject (#8 out...

matlab,plot,signals,circle

I have some random signal (for example sin signal) with the time scale. t=0:0.1:2*pi y=sin(t) plot(t,y) Now I want to draw this signal on this circle. So the time vector actually becomes an envelope of the circle. Envelope of the circle represents "y = 0" in cartesian coordinate system. Here...

string,matlab

I have a string "FDFACCFFFBDCGGHBBCFGE" . Could anyone help me to generate a new string with the same order but no element inside repeated twice. Thanks ! The expected output should be like this : "FDACBGHE"...

python,numpy,matplotlib,graph,plot

I am trying to read one input file of below format. Where Col[1] is x axis and Col[2] is y axis and col[3] is some name. I need to plot multiple line graphs for separate names of col[3]. Eg: Name sd with x,y values will have one line graph and...

matlab,events,delay

In Matlab functions can be started at events,but occasionally, like with the resize function, the events are called in rapid order and the function is called many times in succession, which can cause weird behavior and lag. Is there a way to have it listen for the event but only...

image,matlab,image-processing,computer-vision

I need to save a set of pre-processing images in matlab, resulting in grayscale images. The problem is the fact that these pre-processed images have pixel values higher than 255. If I save them with imwrite() as, for instance, .PNG files, does matlab normalize the values to be in [0,255]...

matlab,loops,for-loop,while-loop,do-while

This is my one dimensional array A. containing 10 numbers A = [-8.92100000000000 10.6100000000000 1.33300000000000 ... -2.57400000000000 -4.52700000000000 9.63300000000000 ... 4.26200000000000 16.9580000000000 8.16900000000000 4.75100000000000]; I want the loop to go through like this; (calculating mean interval wise) - Interval length of 2,4,8 (a(1)+a(2))/2 - value stored in one block of...