Integral average of y values with respect to x values
Show older comments
I have two vectors (x and y) of the same size with x having more than one value in the y direction. For example, x is [0.02 0.02 0.03 0.03 0.03 0.04 0.04 0.05 0.05 0.05] and y is [0.23 0.40 0.12 0.16 0.09 0.50 0.02 0.33 0.10 0.08]. I want to convert the attached scattered plot of the two vectors to a simple continuous curve, by evaluating the integral average of y values with respect to x values. Take into account that the x vector is already ordered from small to large values. How could I do this? Thank you in advance.
1 Comment
Image Analyst
on 5 Oct 2015
"I have two vectors (x and y) of the same size with x having more than one value in the y direction" Huh? Does it have one more, or are x and y the same length? Not sure what you're saying. Are you saying that for any given x value, it may be in the x array more than once, with each occurrence of that x value having a different y value stored at corresponding indexes in the y array?
Accepted Answer
More Answers (1)
[edited to fix typo in comments, and clean-up useless clutter]
Perhaps try combining the filter solution with the accumarray (or filtfilt, if you have the appropriate toolbox, which I do not):
x = Rsorted(:).';
y = Vsorted(:).';
% compute average y for each unique x
[c, ic, ia] = unique(x);
sums = accumarray(ia, y);
wgts = accumarray(ia, ones(size(y))); % weights, i.e. number of reps
avgs = sums./wgts;
% interpolate data on regular grid
n = numel(c); % or some other number
xq = linspace(c(1), c(end), n);
yq = interp1(c, avgs, xq);
% filter gridded data
windowSize = 50;
a = windowSize;
b = ones(1, windowSize);
yf = filter(b, a, yq);
% plot results
hold all;
plot(xq, yf, 'r-', 'LineWidth', 3)

4 Comments
matnewbie
on 6 Oct 2015
arich82
on 6 Oct 2015
If that's what works best for you, that's fine. Just be aware that there is a phase lag when using filter that is eliminated when using filtfilt. (It could be that the simple filter application above simply hasn't had enough time to drop off.)
Have you tried computing the red curve with filtfilt instead of filter? (As I mentioned, it's probably more appropriate, but I don't have that toolbox, and don't trust myself to get the padding correct to implement it myself.)
In general, I think the code above was essentially an implementation of the final suggestion by Star Strider, minus the more-involved filter design (we were apparently typing at the same time, but he submitted first.) As Image Analyst said, applying the filter to the y-data filters by index, not by x-spacing. By chance, the filter may still behave appropriately, but resampling the data onto a regular grid is the only way to explicitly ensure that the standard interpretation of the filter behavior is the correct one.
I hope this helps.
matnewbie
on 7 Oct 2015
Star Strider
on 7 Oct 2015
In my latest Comment (5 Oct 2015 at 21:09), I used filtfilt.
Categories
Find more on Digital Filtering in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
