Search code examples
arraysmatlabcompareaxis-labelscdf

Plotting a vertical line at certain y-value of array MATLAB


Good day,

I have two CDF arrays that I am graphing to compare to each other. I would like to plot a line when the y value is at a certain value for both plotted arrays so that I may compare the x-value variance between the two visually.

Here is a snippet of the code:

    [q, r] = ecdf(ray1_f);
    [w, z] = ecdf(mrc);
    plot(20*log10(r), q, 20*log10(z), w);  
    set(gca, 'YScale', 'log');
    xlabel('dB Relative to Mean');
    ylabel('Cumulative Property');
    title({'Cumulative Probability Distribution Rayleigh and 2 Branch'; 'Max Ratio Combining (filtered)'});
    legend('Rayleigh', '2 Branch Max Ratio');
    xlim([-50 0])

And an example of the output chart:

enter image description here

So, for example, I want to draw vertical lines at the two lines down to the x axis when the y value of the line is equal to 10^-1. I could then use the difference in x value between these two points to determine dB difference.

Me drawing it out in paint:

enter image description here

An example of this could be done with any random array, so I didn't attach more code, but just as an idea of what I'm trying to do.


Solution

  • Say you have two sets of plotting variables x1, y1, x2, y2. In your case

    plot(20*log10(r), q, 20*log10(z), w);
    

    Gives

    x1 = 20*log10(r); y1 = q;
    x2 = 20*log10(z); y2 = w;
    

    The main task is to find the x value for a given y value, the simplest method for which is interpolation using interp1.

    In your example, ypoint = 10^(-1)

    x1point = interp1(y1, x1, ypoint);
    

    This does a simple linear interpolation, there are other options which might suit you, which can be found in the documentation.

    Of course you need to do the same thing for x2,

    x2point = interp1(y2, x2, ypoint);
    

    Now plotting a vertical line is easiest with stem.

    figure; hold on;
    plot(x1, y1, x2, y2);
    stem(x1point, ypoint, 'marker', 'none');
    stem(x2point, ypoint, 'marker', 'none');
    

    Example output:

    stem plot

    Your "dB difference" is obviously then just x2 - x1, or abs(x2 - x1).


    Edit:

    It might be worth noting that to simplify things further, stem can take vector inputs, so the whole code could be:

    xpoints(1) = interp1(y1, x1, ypoint);
    xpoints(2) = interp1(y2, x2, ypoint);
    figure; hold on;
    plot(x1, y1, x2, y2);
    stem(xpoints, ones(size(xpoints))*ypoint, 'marker', 'none');