Matlab polyfit for a region of data - matlab

I have a set of data I need to generate two linear best fit lines for (1st order polyfit) but I don't know how to specify which region each line should fit data to. I need one line in the region between minimum x value and 0 and the other one in the region 0.25 < x.
Also, in the second region, there are two clear areas of data, one above the other, and I need the best fit line to be fitted only to the lower one.
I am a complete novice at Matlab so any help would be greatly appreciated
%load data, force and velocity
load ('exp_6_Force');
load ('exp_6_Velocity');
% Give a name to the title bar.
set(gcf,'name','Experiment 6 velocity','numbertitle','off')
%set variables to x and y
x = Force;
y = Velocity;
%plot the graph
plot(x,y);
%add grid and legend
grid on;
legend ('Velocity');
%add labes and title
xlabel ('Force');
ylabel ('Velcoity');
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
%find coordinates of y min point
[value,index1] = min(y);
yminxcoor = x(index1);
yminycoor = y(index1);

Use the logical index to get the x and y data for the two regions:
For region 1:
x_region1 = (x<0).*x
y_region1 = (x<0).*y
For region 2:
x_region2 = (x>0.25).*x
y_region2 = (x>0.25).*y
Then you can polyfit in these regions
For region 1:
p_region1 = polyfit(x_region1, y_region1, 1)
For region 2:
p_region2 = polyfit(x_region2, y_region2, 1)

Related

Change color of specific points in matlab?

I have a scatterplot in Matlab and was wondering if there was any way to the change the color of just one of the points?
you can plot the graph, after that replot the point which you want.
% plot the curve or graph
hold on
plot(x,y,'.r')
Try to hold on and then plot the point (x,y) which you want with the specified color (r) which you are interested in.
If you do not want to overlay a second plot on the first, you can plot each point individually and use handles. In that way, you can later perform arbitrary changes on each individual point.
You can find an example below.
% Generate some numbers
x = randn(10,1);
y = randn(10,1);
% Plot each point individually
figure
hold on
for idx = 1 : numel(x)
hdl(idx) = plot(x(idx),y(idx),'marker','.','color','k')
end
% change color, markerstyle, x-position, etc...
hdl(2).Color = [1 0 0]
hdl(3).Marker = 'o'
hdl(5).XData = 1
x = rand(10,1) ;
y = rand(10,1) ;
scatter(x,y) ;
[x1,y1] = getpts ;
hold on
plot(x1,y1,'Or') ;
click on the point, you want to change color, when prompted.

In Matlab, how to draw lines from the curve to specific xaxis position?

I have a spectral data (1000 variables on xaxis, and peak intensities as y) and a list of peaks of interest at various specific x locations (a matrix called Peak) which I obtained from a function I made. Here, I would like to draw a line from the maximum value of each peaks to the xaxis - or, eventually, place a vertical arrow above each peaks but I read it is quite troublesome, so just a vertical line is welcome. However, using the following code, I get "Error using line Value must be a vector of numeric type". Any thoughts?
X = spectra;
[Peak,intensity]=PeakDetection(X);
nrow = length(Peak);
Peak2=Peak; % to put inside the real xaxis value
plot(xaxis,X);
hold on
for i = 1 : nbrow
Peak2(:,i) = round(xaxis(:,i)); % to get the real xaxis value and round it
xline = Peak2(:,i);
line('XData',xline,'YData',X,'Color','red','LineWidth',2);
end
hold off
Simple annotation:
Here is a simple way to annotate the peaks:
plot(x,y,x_peak,y_peak+0.1,'v','MarkerFaceColor','r');
where x and y is your data, and x_peak and y_peak is the coordinates of the peaks you want to annotate. The add of 0.1 is just for a better placing of the annotation and should be calibrated for your data.
For example (with some arbitrary data):
x = 1:1000;
y = sin(0.01*x).*cos(0.05*x);
[y_peak,x_peak] = PeakDetection(y); % this is just a sketch based on your code...
plot(x,y,x_peak,y_peak+0.1,'v','MarkerFaceColor','r');
the result:
Line annotation:
This is just a little bit more complicated because we need 4 values for each line. Again, assuming x_peak and y_peak as before:
plot(x,y);
hold on
ax = gca;
ymin = ax.YLim(1);
plot([x_peak;x_peak],[ymin*ones(1,numel(y_peak));y_peak],'r')
% you could write instead:
% line([x_peak;x_peak],[ymin*ones(1,numel(y_peak));y_peak],'Color','r')
% but I prefer the PLOT function.
hold off
and the result:
Arrow annotation:
If you really want those arrows, then you need to first convert the peak location to the normalized figure units. Here how to do that:
plot(x,y);
ylim([-1.5 1.5]) % only for a better look of the arrows
peaks = [x_peak.' y_peak.'];
ax = gca;
% This prat converts the axis unites to the figure normalized unites
% AX is a handle to the figure
% PEAKS is a n-by-2 matrix, where the first column is the x values and the
% second is the y values
pos = ax.Position;
% NORMPEAKS is a matrix in the same size of PEAKS, but with all the values
% converted to normalized units
normpx = pos(3)*((peaks(:,1)-ax.XLim(1))./range(ax.XLim))+ pos(1);
normpy = pos(4)*((peaks(:,2)-ax.YLim(1))./range(ax.YLim))+ pos(2);
normpeaks = [normpx normpy];
for k = 1:size(normpeaks,1)
annotation('arrow',[normpeaks(k,1) normpeaks(k,1)],...
[normpeaks(k,2)+0.1 normpeaks(k,2)],...
'Color','red','LineWidth',2)
end
and the result:

Matlab Plotting roots (zeros and poles) of Z-Transform

I need to plot the roots onto a transfer function H(z) overlaying a unit circle, giving enough room to see all points. I'm able to get the roots from H(z) when it is given in the form zeros = [z0 z1 z2...], poles = [p0 p1 p2]. Using Matlab's roots function, I'm able to get the pole and zero locations. My Matlab code so far is
function zplot(b, a)
b_roots = roots(b);
a_roots = roots(a);
hold on
rectangle('Position',[-1 -1 2 2],'Curvature',[1 1]);
plot(b_roots,'x blue');
plot(a_roots,'o blue');
axis %need axis to be equal and +10percent of maximum value
hold off
end
So far, I can plot the roots and the unit circle, but I need help with adjusting the axes so that they are 1) equal to each other and 2) 10% more than the highest value. I'm not sure how to go about doing this part. I tried making a variable lim_max = max(b_roots,a_roots) but it ended up being an array, and wouldn't work in the axis([-lim_max lim_max -lim_max lim_max]) function. I need the plot to scale to +10% with the inputs as they change.
Side note (not necessary): is there a way for it to look like a circle when I plot it, because right now it ends up looking like an oval most of the time. I can readjust the screen, which is fine, but if there's an easy way to do that, I'd also like to know.
Set axis equal and calculate min/max:
function zplot(b, a)
b_roots = roots(b);
a_roots = roots(a);
xlimits = [min(min([real(a_roots);real(b_roots)])), max(max([real(a_roots);real(b_roots)]))];
ylimits = [min(min([imag(a_roots);imag(b_roots)])), max(max([imag(a_roots);imag(b_roots)]))];
hold on
rectangle('Position',[-1 -1 2 2],'Curvature',[1 1]);
plot(b_roots,'x black');
plot(a_roots,'o blue');
axis equal;
xlim(1.1*xlimits);
ylim(1.1*ylimits);
hold off
end
Use the following code. This will 1) find the maximum overall limits of x and y axes 2) set those limits equal to each other 3) plot those limits +10%
b_roots = roots(b);
a_roots = roots(a);
x_min = min(min([real(a_roots);real(b_roots)]));
x_max = max(max([real(a_roots);real(b_roots)]));
y_min = min(min([imag(a_roots);imag(b_roots)]));
y_max = max(max([imag(a_roots);imag(b_roots)]));
%get the magnitude of the overall minimum value
min_lim = abs(min(x_min,y_min));
%abs may not be necessary
max_lim = abs(max(x_max,y_max));
%set high and low limits equal to each other from negative to positive
eq_limit = [-max(min_lim,max_lim),max(min_lim,max_lim)];
hold on
rectangle('Position',[-1 -1 2 2],'Curvature',[1 1]);
plot(b_roots,'x black');
plot(a_roots,'o blue');
axis equal;
xlim(1.1*eq_limit);
ylim(1.1*eq_limit);
hold off
Thanks to #M.S. for their answer and help.

Find centroid in image region

I have a set of matrixes with a number of blobs (this changes from matrix to matrix), and I'm looking for a way to find the center of mass of the blobs. To do so I use regionprops with the Centroid option.
B = bwlabel(A);
stat = regionprops(B,'Centroid');
number_centers = numel(stat);
coord_centers = zeros(number_centers,2);
xy_cylinder = zeros(number_centers,2);
for x = 1: number_centers
coord_centers(x,1) = stat(x).Centroid(1); % Angle
coord_centers(x,2) = stat(x).Centroid(2); % Radius
end
If I plot the matrix with the centroids superimposed (code below), I noticed that they slightly offset (they are on the corner of the pixel where the centroid is). Do you think I'm right? And do you know why is that?
axis equal;
pcolor(A), colorbar, hold on;
xlabel('Angle'); ylabel('Radius');
for x = 1: numel(stat)
plot(stat(x).Centroid(1),stat(x).Centroid(2),'r+');
end

How to draw a straight across the centroid points of the barcode using best fit points Matlab

This is the processed image and I can't increase the bwareaopen() as it won't work for my other image.
Anyway I'm trying to find the shortest points in the centre points of the barcode, to get the straight line across the centre points in the barcode.
Example:
After doing a centroid command, the points in the barcode are near to each other. Therefore, I just wanted to get the shortest points(which is the barcode) and draw a straight line across.
All the points need not be join, best fit points will do.
Step 1
Step 2
Step 3
If you dont have the x,y elements Andrey uses, you can find them by segmenting the image and using a naive threshold value on the area to avoid including the number below the bar code.
I've hacked out a solution in MATLAB doing the following:
Loading the image and making it binary
Extracting all connected components using bwlabel().
Getting useful information about each of them via regionprops() [.centroid will be a good approximation to the middel point for the lines].
Thresholded out small regions (noise and numbers)
Extracted x,y coordinates
Used Andreys linear fit solution
Code:
set(0,'DefaultFigureWindowStyle','docked');
close all;clear all;clc;
Im = imread('29ekeap.jpg');
Im=rgb2gray(Im);
%%
%Make binary
temp = zeros(size(Im));
temp(Im > mean(Im(:)))=1;
Im = temp;
%Visualize
f1 = figure(1);
imagesc(Im);colormap(gray);
%Find connected components
LabelIm = bwlabel(Im);
RegionInfo = regionprops(LabelIm);
%Remove background region
RegionInfo(1) = [];
%Get average area of regions
AvgArea = mean([RegionInfo(1:end).Area]);
%Vector to keep track of likely "bar elements"
Bar = zeros(length(RegionInfo),1);
%Iterate over regions, plot centroids if area is big enough
for i=1:length(RegionInfo)
if RegionInfo(i).Area > AvgArea
hold on;
plot(RegionInfo(i).Centroid(1),RegionInfo(i).Centroid(2),'r*')
Bar(i) = 1;
end
end
%Extract x,y points for interpolation
X = [RegionInfo(Bar==1).Centroid];
X = reshape(X,2,length(X)/2);
x = X(1,:);
y = X(2,:);
%Plot line according to Andrey
p = polyfit(x,y,1);
xMin = min(x(:));
xMax = max(x(:));
xRange = xMin:0.01:xMax;
yRange = p(1).*xRange + p(2);
plot(xRange,yRange,'LineWidth',2,'Color',[0.9 0.2 0.2]);
The result is a pretty good fitted line. You should be able to extend it to the ends by using the 'p' polynomal and evaluate when you dont encounter any more '1's if needed.
Result:
If you already found the x,y of the centers, you should use polyfit function:
You will then find the polynomial coefficients of the best line. In order to draw a segment, you can take the minimal and maximal x
p = polyfit(x,y,1);
xMin = min(x(:));
xMax = max(x(:));
xRange = xMin:0.01:xMax;
yRange = p(1).*xRange + p(2);
plot(xRange,yRange);
If your ultimate goal is to generate a line perpendicular to the bars in the bar code and passing roughly through the centroids of the bars, then I have another option for you to consider...
A simple solution would be to perform a Hough transform to detect the primary orientation of lines in the bar code. Once you find the angle of the lines in the bar code, all you have to do is rotate that by 90 degrees to get the slope of a perpendicular line. The centroid of the entire bar code can then be used as an intercept for this line. Using the functions HOUGH and HOUGHPEAKS from the Image Processing Toolbox, here's the code starting with a cropped version of your image from step 1:
img = imread('bar_code.jpg'); %# Load the image
img = im2bw(img); %# Convert from RGB to BW
[H, theta, rho] = hough(img); %# Perform the Hough transform
peak = houghpeaks(H); %# Find the peak pt in the Hough transform
barAngle = theta(peak(2)); %# Find the angle of the bars
slope = -tan(pi*(barAngle + 90)/180); %# Compute the perpendicular line slope
[y, x] = find(img); %# Find the coordinates of all the white image points
xMean = mean(x); %# Find the x centroid of the bar code
yMean = mean(y); %# Find the y centroid of the bar code
xLine = 1:size(img,2); %# X points of perpendicular line
yLine = slope.*(xLine - xMean) + yMean; %# Y points of perpendicular line
imshow(img); %# Plot bar code image
hold on; %# Add to the plot
plot(xMean, yMean, 'r*'); %# Plot the bar code centroid
plot(xLine, yLine, 'r'); %# Plot the perpendicular line
And here's the resulting image: