3

I have several data points that are plotted below and I would like to find the frequency value when the amplitude value crosses 4. I've included an example along with the data points in the example below. I've circled the answer graphically but I'm not sure how to compute it mathematically and get all the values for the frequencies I desire. How can I do this with octave / matlab? Also is there a mathematical term for what I'm trying to do?

In this example I'm trying to get 5 frequencies (but this is just an example) I know two answers are 30 and 80 but not sure how to get the rest. The full list could be thousands. I'm using octave 3.8.1

clear all,clf, clc,tic
%graphics_toolkit gnuplot %use this for now it's older but allows zoom
freq=[20,30,40,50,60,70,80];
amp_orig=[2,4,3,7,1,8,4];
amp_inv=[6,4,5,1,7,0,4];


plot(freq,amp_orig,'-bo')
hold on
plot(freq,amp_inv,'-r*')
xlabel ("Frequency");
ylabel ("Amplitude");

enter image description here

Thanks

Rick T
  • 3,349
  • 10
  • 54
  • 119
  • 2
    Does this help? http://www.mathworks.com/matlabcentral/fileexchange/27205-fast-line-segment-intersection – amo Sep 22 '14 at 23:16
  • To answer your other question, what you're trying to do is find the intersection of the lines. [This](http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect) question gives a good overview of various techniques. – MrAzzaman Sep 22 '14 at 23:35
  • @amo thanks but this function will not work with octave 3.8.1 – Rick T Sep 23 '14 at 11:51

2 Answers2

2

Should you have access to Matlabs Mapping Toolbox, your problem can be solved easily using the function polyxpoly(). It will find the intersections of two graphs. They do not even have to be lines, also polygons work. Here is an example for your case:

%graphics_toolkit gnuplot %use this for now it's older but allows zoom
freq=[20,30,40,50,60,70,80];
amp_orig=[2,4,3,7,1,8,4];
amp_inv=[6,4,5,1,7,0,4];

plot(freq,amp_orig,'-bo')
hold on
plot(freq,amp_inv,'-r*')
xlabel ('Frequency');
ylabel ('Amplitude');

%// find and add intersections
[xi,yi] = polyxpoly(freq,amp_orig,freq,amp_inv)
plot(xi, yi,'ro','markersize',20) %// draw the red circles automatically :-)

The result looks like this enter image description here

Nras
  • 4,251
  • 3
  • 25
  • 37
  • 1
    Thanks but I'm using octave and it looks like the the mapping toolbox doesn't have the polyxpoly function yet – Rick T Sep 23 '14 at 11:50
  • @RickT Right, ``polyxpoly()`` is part of the Mapping Toolbox, I should have mentioned that in the answer. Too bad, this would have made your problem really easy. – Nras Sep 23 '14 at 12:12
1

Try this function from the File Exchange, it seems to work just fine in Octave. x0 are the frequencies of interest:

>> [x0,y0,iout,jout] = intersections(freq,amp_orig,freq,amp_inv)
x0 =

   30.000
   30.000
   42.500
   55.000
   64.286
   80.000

y0 =

   4.0000
   4.0000
   4.0000
   4.0000
   4.0000
   4.0000

iout =

   2.0000
   2.0000
   3.2500
   4.5000
   5.4286
   7.0000

jout =

   2.0000
   2.0000
   3.2500
   4.5000
   5.4286
   7.0000
am304
  • 13,758
  • 2
  • 22
  • 40