How to create an average of 2 lines on a single graph

26 views (last 30 days)
Hi,
I have two lines which represent magnetic intensity data collected along a transect back and forth, hence the 2 lines, producing this figure;
% Single-line Magnetic Anomaly graph, Charlie Hillary 2021
% load top and bottom data files
top = load('top_sensor_data.txt')
% calculate the temporal gradient
dtopdt = (top(end,3)-top(1,3))/(top(end,2)-top(1,2)) ;
% correct the data
top(:,3) = top(:,3) - dtopdt*(top(:,2)-top(1,2)) ;
% plot
figure
plot(top(1:end-1,1),top(1:end-1,3)-48e3,'k-') ;
xlabel('Distance / m')
ylabel('Upper B / nT ') ;
title('Top @ 0.95m')
I need to create an average of the 2 lines. I have a rough understanding of how to do so. I think I need to create a loop for each Y value(s) at a given X value, and create an average of the two, by adding and dividing by two. The data is a 3 column array, with column 2 being used to process column 1 (x) and column 3 (y).
Just need a point in the right direction, thanks!
Charlie
  4 Comments
dpb
dpb on 7 Mar 2021
Edited: dpb on 7 Mar 2021
"...if you run this code"
With what for input data?
plot(top(1:end-1,1),top(1:end-1,3)-48e3,'k-') ;
is the only plot command in the code; unless the first column does represent a triangle waveform of
[0:Xmax fliplr[0:Xmax]]
but we can't know that, if so.
What's with the magic number of 48000? Especially after the earlier comment that you "corrected" it???
All in all, is pretty confusing to the outsider... :)
Charlie Hillary
Charlie Hillary on 7 Mar 2021
Oh my bad you don't have the .txt file! Here it is. The -48000 is to remove the average background magnetic intensity for the location of the transect.
As you can see the data is also collected whilst returning from the end point in the transect generating 2 y values per given x value. I need these to me merged, but wasn't sure on how to do this with polyfit.
Charlie

Sign in to comment.

Accepted Answer

dpb
dpb on 7 Mar 2021
OK, I see...the data are collected on a up- and down-slope symmetric ramp..
Try something like--
top=readmatrix('top_sensor_data.txt');
top=reshape(top,length(top)/2,[]);
top(:,[2:2:end])=flipud(top(:,2:2:end));
top=array2table(top(:,[1 3:end]),'VariableNames',{'Distance','V1_Out','V1_In','B_Out','B_In'});
>> head(top)
ans =
8×5 table
Distance V1_Out V1_In B_Out B_In
________ ______ ______ ________ ________
15.00 0.50 495.00 48700.67 48699.92
16.00 2.25 499.09 48701.12 48699.82
17.00 3.75 486.75 48700.58 48699.33
18.00 5.25 485.75 48701.99 48693.28
19.00 7.00 484.75 48702.83 48703.04
20.00 8.50 483.75 48703.13 48702.60
21.00 10.00 482.75 48703.30 48701.68
22.00 11.50 482.00 48703.92 48703.99
hAx(1)=subplot(2,1,1);
plot(top.Distance,[top.B_Out top.B_In])
legend('B Out','B In','Location','southwest')
xlim([0 265])
hAx(2)=subplot(2,1,2);
plot(top.Distance,detrend([top.B_Out top.B_In]))
xlim([0 265])
mnB=mean([top.B_Out top.B_In],'all')
>> mnB =
48685.42
>> mean([top.B_Out top.B_In])
ans =
48686.39 48684.44
>> diff(ans)
ans =
-1.95
>>
yyaxis right
hL=plot(top.Distance,[top.B_Out top.B_In]-mnB);
legend('B Out Detrended','B In Detrended','B Out Less Mean','B In Less Mean','Location','southwest')
This shows a very nearly flat profile going across as the small bias between the detrended and mean-subtracted signals show--what's the best thing to do I've no klew having no knowledge of what the data are (nor the field if did :) ).
Oh -- I realize I completely got carried away with the pretty figures and totally forget the actual Q? -- alhough by now it ought to be apparent how to do the mean of the two--
top.B_Avg=mean([top.B_Out top.B_In],2);
>> head(top)
ans =
8×6 table
Distance V1_Out V1_In B_Out B_In B_Avg
________ ______ ______ ________ ________ ________
15.00 0.50 495.00 48700.67 48699.92 48700.29
16.00 2.25 499.09 48701.12 48699.82 48700.47
17.00 3.75 486.75 48700.58 48699.33 48699.96
18.00 5.25 485.75 48701.99 48693.28 48697.64
19.00 7.00 484.75 48702.83 48703.04 48702.94
20.00 8.50 483.75 48703.13 48702.60 48702.86
21.00 10.00 482.75 48703.30 48701.68 48702.49
22.00 11.50 482.00 48703.92 48703.99 48703.95
>>
Not knowing what to do with the second variable, I simply carried it along for the ride...
  2 Comments
Charlie Hillary
Charlie Hillary on 8 Mar 2021
I see! Thank you, that is clear and easy to visualise the solution from showing the arrays your code produces.
I plotted B_Avg against dist, and removed the background signal of 48000nT.
For your understanding, the data is collected along a transect above ground, and is collecting info on the magnetic intensity of the ground beneath at a given distance. The large trough is a negative magnetic anomaly, where there is a lack of bedrock, or perhaps a large infilled hole (where historically a quarry once lie).
From this figure you can calculate the limiting depth (maximum depth the anomaly can lie in order to produce that anomaly), which is the distance from the middle of the trough to the outer trough, from the half height of the trough (at roughly 550 nano Teslas), which is about 5m.
Charlie
dpb
dpb on 8 Mar 2021
Thanks for the background...interesting to know what one is looking at!
Why not remove the actual mean instead of something approximating it?
And, detrend does that along with compensating for a trend; not knowing the reason(s) behind any trend there might be; I already cleared the data but iirc the slope is pretty small; that's indicative in how well the two overlay when plot detrend(B) over B-mn(B).

Sign in to comment.

More Answers (1)

David Goodmanson
David Goodmanson on 8 Mar 2021
Edited: David Goodmanson on 8 Mar 2021
Hi Charley
since the second half of the distance array has exactly the same values as the first half (only reversed), you can use just the first half of the distance array for plotting purposes. After you make the data correction to top3, then
dist = top(1:245,1);
B1 = top(1:245,3);
B2 = flip(top(246:end,3));
Bmean = mean([B1 B2],2);
plot(dist,B1,'b',dist,B2,'b',dist,Bmean,'r')
xlabel('Distance / m')
ylabel('Upper B / nT ') ;
title('Top @ 0.95m')
Here B1 and B2 are column vectors. The square brackets concatenate them side-by-side, creating a 245x2 matrix. The '2' in the mean command says to take the mean across rows, rather than down columns which is the default. So you get the mean of the two curves.
  1 Comment
Charlie Hillary
Charlie Hillary on 8 Mar 2021
Hi David,
This also worked well for me. I thought it would be difficult splitting the .txt file! Much simpler than I imagined, and an intuitive response so thanks.
Charlie

Sign in to comment.

Categories

Find more on Colormaps in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!