Binning/averaging data in Matlab - matlab

I'm currently trying to solve this; I have an idea of how to do this, but I would really appreciate any extra input I could get.
I need to come up with a Binning function that takes in raw thickness data from a trace file, that is read in by another function. I need to put this data in 5mm bins. The tricky part will be to smooth fit the curves at both ends, and also perform data validation of the input data.
In summary: I need to take data values and "bin"/average them in 5mm intervals.
Thanks guys.

It sounds like you want the hist() function, which creates a histogram by binning the input data.

Related

Extracting different values of a function vs time using MATLAB curve fitting

I feel like this should be something simple to solve - but I'm struggling to find the answer anywhere.
I have a set of 'R' values and a set of time values, I want to use curve fitting (I haven't used this part of the software before) to calculate the 'R' values at a different set of time values, literally just be able to access what is displayed in a figure created using curve fitting using a different set of time values (ie I can point the curser to the values I want on a figure and write them down but this is not efficient at all for the number of time values I have). Context is an orbital motion radius vs time.
Thanks in advance :)
You can use Matlab's fit function to do this very easily. Assuming you have your data in arrays r and t, you can do something like this:
f = fit(t, r, 'smoothingspline')
disp(f(5))
If you consult the documentation, you can see the various fit types available. (See https://www.mathworks.com/help/curvefit/fit.html)

Best way to plot large set of data from multiple files matlab

I have a very huge set of data which needs to be imported from different files and need to plot in one figure(at most 5 plots). Right now I am doing this in a for loop which is really slowing up the process. My idea is to make two matrices one contains 'x' values and another with 'y' values and finally plot. The data size is not the same. What is the best way to achieve this? Can this be done using arrayfun or vectorization?

fastest way to display huge 2D data in MATLAB

I have a huge ~9Gb .bin file.
Reading data with fread(), getting 2D array A ~ 10^9 points.
Trying to display with imagesc() as simple as:
figure(1)
imagesc(x,y,A)
It takes ~ 800 seconds for me and I can't see anything on the figure.
I am sure that I read the file right. Checked with smaller ones.
So I wonder is there a way to display such a huge data with less effort for my PC?
Perhaps use some kind of downsampling on A. To do it right you'd have to apply a low-pass filter followed by decimation, but the low-pass filter may take very long in your case. So, even if it's subject to possible aliasing, you can try to just take a sample out of n and plot that:
n = 10; %// choose as suits you best
imagesc(x(1:n:end), y(1:n:end), A(1:n:end,1:n:end))
It is quite hard to answer your question without knowing the nature of the data.
Here are some ideas:
If your data is an image, you should downscale it using on of the known methods, or crop it.
If you know that your data is smooth, you can sample it without introducing aliasing.
Show some kind of statistics on your data, instead of showing the data itself.

How Do I Use fft() Function In Matlab To Find The Frequency For A Set Of Data Points?

I've been given a set of about 20k data points. I managed to import the data into Matlab and did the following :
importdata;
fft(importdata);
and it returns :
Undefined function 'fft' for input arguments of type 'cell'
Now, I understand that I need more than this to get it working.
Can someone please tell me any more parameters I need for the fft and how to implement it?
Edit: These datapoints are timestamps of when something is detected in a machine,
I'm trying to find if there is a period of the detection occuring.
Look at the documentation for the correct way to use importdata and fft. Both have examples to get you started.
First you need to assign the output of importdata to a matrix, then do any data manipulation you need to get it in a form to be used by fft. Finally, use fft with the correct parameters and compute the correct frequency vector as per the example (obviously, adapted to your data)

How to deal with video frames in simulink

I am working on videos in simulink.As we know that multimedia file block reads one frame at a time so when I will attach it with matlab function block it should read one frame at a time with imread command. If I double click the matlab function block (as shown at http://tinypic.com/view.php?pic=dggujd&s=6) then you will see that I have to give the name of the function as written in matlab mfile along with the input; which in this case is video(named as convid.avi). I am reading one frame at a time but I have given the whole video as an argument to the matlab function.This is the problem how can I resolve it.What should be given as an argument in matlab function block rather than the whole video.I have also uploaded my model at http://tinypic.com/view.php?pic=55jggw&s=6. The code which I am using for vidfunc is:
function h=vidfunc(u)
a=imread(u); % read frame
BW = edge(a,'sobel'); %sobel edge detection
[H,thetaa,rhoo]=hough(BW); % Hough Transform
P = houghpeaks(H,6,'threshold',ceil(0.5*max(H(:))));
lines=houghlines(BW,thetaa,rhoo,P,'FillGap',15,'Minlength',15)
figure,imshow(I),hold on
for k = 1:length(lines) % Draw lines
xy = [lines(k).point1; lines(k).point2];
z(k)=lines(k).point2(2);
plot(xy(:,1),xy(:,2),'LineWidth',1,'Color','green');
end
h=z(k)
It looks like the meat of the work done in this "Simulink" model is actually being performed by your MATLAB function. So the answer to this question is going to largely rely on what that function is actually doing. Specifically, what is the expected input to vidfunc, and what is this function's output? I suspect that this function may need to be revised to fit into your model.
To debug your model, it's useful to think about what the signal output is from each block. At each time step, your From Multimedia File block will output a single image frame, which according to the doc looks to be structured as an
M-by-N-by-P color video signal where P is the number of color planes.
Moving downstream, we next come to the Color Space Conversion block, which in this case looks like it will most likely output an image frame in the form of an M-by-N matrix (where each element of the matrix corresponds to the image's intensity at that pixel).
Now we come to the interesting part -- the MATLAB Fcn block. As we just saw, the input to this block will be an M-by-N matrix representing a single image frame. When you look in the parameter dialog box for the MATLAB Fcn block, the input to this block is represented by the variable u. Therefore, to execute the vidfunc function on the image frame being input to this block, you would simply enter vidfunc(u) for your MATLAB function.
Now, based on the input going to the MATLAB Fcn block, and the fact that you have a Video Viewer block connected to the output, vidfunc should be structured such that it operates on a single image frame as its input and outputs another single image frame. If vidfunc is not structured in this way, you will need to edit it (or just re-implement the same functionality using Simulink blocks).
That said, let's assume that vidfunc is also returning an M-by-N matrix representing a processed image frame. You will want to set the Output Dimensions parameter for your MATLAB Fcn block to -1 to indicate that the output will have the same dimensions as the input. Also, (as indicated in the doc) you will want to make sure Collapse 2-D results to 1-D is unchecked or else your image output will be in the form of one long vector rather than an M-by-N matrix.
Provided that vidfunc is structured correctly, this should solve your problem.
NOTE: To make your life a lot easier, I would strongly suggest displaying the signal data type and dimensions in your Simulink model. This can help to avoid a lot of confusion. This doc describes exactly how to do this.
--UPDATE--
After looking at your code, this confirms my suspicion that the inputs/outputs of vidfunc are inconsistent with what your Simulink model expects. The way that you proceed is highly dependent on what your own design constraints are and what you actually want out of this system. Basically, your Simulink model and MATLAB function disagree... which one is right? I'll give some general thoughts based on my best guesses at what you're aiming for.
First, Simulink is passing an image (in the form of an M-by-N matrix) into vidfunc. This means that vidfunc no longer needs to load an image at the start of the code. So I believe that you could update the first few lines of code to be as such:
function h=vidfunc(a)
BW = edge(a,'sobel'); %sobel edge detection
See that now vidfunc is taking the actual image (not the filename that contains the image) as its input. Basically, you are removing the a=imread(u); line and jumping right in to processing a.
The other issue is the output of vidfunc. Simulink is expecting the output to be an image, but it is not. I'm not 100% sure what h is supposed to be in this code (when I first glanced at your code I thought these were handles to line objects but that doesn't seem to be the case). It looks to possibly be the y coordinate of the endpoint of one of the houghlines. Nevertheless, it's not what your Simulink model is expecting. This one is not as straight-forward to fix. Possibly you could try to use getframe to grab an image from the plot of your lines.
I actually feel that the best advice that I can give to you is to just scrap the MATLAB function and implement everything in Simulink. I think this will be a lot easier than trying to get vidfunc to play nicely with your model. vidfunc does not actually contain that much code, so this should not be too difficult of a task for you. Another benefit is that at the end of this process you'll have a nice Simulink model that explicitly shows all of the image processing steps that you are taking.
I believe that all of the image processing that you are doing with MATLAB functions can also be done with Simulink blocks (see the Simulink Blocks section of this doc).
Good luck.