Frequency measurement in LTspice - Page 1
Forum section: electronics / projects

author: WaveyDipole
author_rank: Frequent Contributor
timestamp: August 30, 2017, 10:19:45 am
content: Firstly let me say that I have attempted to post this topic on the LTspice Yahoo group, but since there seems to be no way to post pictures it is difficult to explain the issue so I thought it might be more fruitful to post it here together with images.I have recently been experimenting with a number of simple circuits and am beginning to enjoy this tool. The most recent example was an RF oscillator circuit and I am trying to measure the frequency of the generated audio pilot tone. I am using the method described here:http://www.electro-tech-online.com/threads/measuring-frequency-with-ltspice.118988/The problem is that the readings I was getting didn't make any sense and didn't change much when I changed the component values, so I put the matter to the test by using a simple 1khz sinewave generator circuit:http://www.stufinnis.co.uk/oscillator.htmlI chose this circuit because it uses the same arrangement as the audio tone section of the RF oscillator circuit that I am trying to work with, just that it operates using 9v instead of 15v. Instead of getting 1khz at the output, I was getting a reading of around 10hz! Is the problem the method I am using or is LTspice displaying erroneous data?BTW, I should also mention that Dave's video on using LTspice provided a great overview and was very helpful. Thanks for making it available.https://www.eevblog.com/2013/09/07/eevblog-516-ltspice-tutorial-dc-operating-point-analysis/

author: AlfBaz
author_rank: Super Contributor
timestamp: August 30, 2017, 10:40:47 am
content: Have a closer look at what the cursors are actually reading, the time difference between them (period) and from that inverting to give you frequency.To measure frequency with cursors, zoom in to a cycle or two and place the cursors on consecutive peaks

author: RoGeorge
author_rank: Super Contributor
timestamp: August 30, 2017, 10:42:50 am
content: That box does not magically detect the oscillations, it just shows whatever distance you selected between the cursors.In the picture, it shows the measured distance between your cursor placement (92 ms) and "Freq:" is "1/whatever you selected", in this case is 1/92ms ~ 10Hz.If you want to measure the frequency of the oscillations, you need to place the cursors on two consecutive peaks of the oscillations.

author: WaveyDipole
author_rank: Frequent Contributor
timestamp: August 30, 2017, 10:58:14 am
content: Thanks! Yes - this is the bit in the instruction that I missed!QuoteMeasurement is indeed correct when exactly one cycle is selected. This requires a little messing around with the simulation time parameters to get a sensible display that allows one cycle to be easily picked out on the graph using the markers.Is there not a simpler way to calculate frequency in LTspice using parameters?

author: Zero999
author_rank: Super Contributor
timestamp: August 30, 2017, 11:09:17 am
content: Quote from: WaveyDipole on August 30, 2017, 10:58:14 amOr you can zoom into the plot window, to see more detail.As far as I'm aware, there's no way to get it do to it for you.

author: WaveyDipole
author_rank: Frequent Contributor
timestamp: August 30, 2017, 11:15:40 am
content: AlfBaz mentioned zooming into a cycle or two and I have just I discovered that I can do this by holding down Ctrl and rotating the mouse wheel, but this is not as precise and it gets tricky to pick out a cycle if when you zoom in far enough to get separation between the cycles but then the peaks are chopped off. Is it possible to zoom just the time axis while keeping amplitude the same?

author: nugglix
author_rank: Regular Contributor
timestamp: August 30, 2017, 11:24:05 am
content: Just mark the part to zoom into by click-and-drag.So in the plot window press the mouse button and move the mouse.You'll see a box opening which defines the part to zoom into.By releasing the mouse button the plot window will be updated.Btw, there are a lot of nice tutorials for LTSpice available.

author: Ian.M
author_rank: Super Contributor
timestamp: August 30, 2017, 11:34:47 am
content: Unfortunately LTspice doesn't preserve the cursor positions when you zoom the plot so its really tricky to get an accurate frequency manually, and a total PITA if you need to measure it repeatedly while you adjust the design.Its fairly trivial to automate the frequency measurement with some .measure statements.Code:* Measure Frequency* See SPICE Error Log for result.meas tran T1 find time when V(v)=0 rise 1.meas tran T2 find time when V(v)=0 rise 2.meas tran Frequency param 1/(T2-T1)measures the frequency of waveform V(v) by taking the reciprocal of the period between its first and secondrisingzero crossings of the plot. Replace V(v) with your voltage or current waveform. Change the =0 if your signal has a DC offset.Unfortunately LTSPICE *SUCKS* at displaying .measured results - you have to dig in the SPICE error log for them and keep track of the units yourself.If you need to measure the frequency at a later cycle either increase both counts after 'rise' by the same amount, or use the .tran Tstart (Time To Start Saving Data) parameter. If you want to average over ten cycles for greater accuracy use:Code:* Measure Frequency* See SPICE Error Log for result.meas tran T1 find time when V(v)=0 rise 1.meas tran T2 find time when V(v)=0 rise 11.meas tran Frequency param 10/(T2-T1)Results from sim below - extract from error logCode:t1: time=0.305055 at 0.305055t2: time=0.623376 at 0.623376frequency: 1/(t2-t1)=3.14148P.S. When asking about a LTspice sim, *PLEASE* post your .asc simulation file (and either provide or give details of where to get any non-standard models or symbols), and also, for anything involving multiple signals or complicated measurements, your .plt plot settings file. If you only post graphics its *MUCH* harder for viewers to reproduce your sim so most people wont bother.

author: AlfBaz
author_rank: Super Contributor
timestamp: August 30, 2017, 11:36:47 am
content: Quote from: WaveyDipole on August 30, 2017, 11:15:40 amDo as nugglix suggested to zoom in. If zooming in messes up the height of your waveform then click on the autorange button

author: AndyC_772
author_rank: Super Contributor
timestamp: August 30, 2017, 12:06:33 pm
content: To measure the frequency of a waveform, either in Spice or on a scope screen, don't try to measure from one peak to another.Instead, always measure to the point where the waveform crosses a fixed threshold, at the point where dV/dt is greatest. Usually this is in the centre of the waveform, as it crosses zero (or whatever the mean dc level happens to be, in case there's an offset).

author: Kalvin
author_rank: Super Contributor
timestamp: August 30, 2017, 12:38:41 pm
content: If you do not need cycle accurate measurement, you can build something like this:https://pdfs.semanticscholar.org/96dc/103fa3bc8e811d847b34c37d473754be9f92.pdfI guess this will take only some B-blocks using Laplace transfer functions. Save the design as a macro to be used in other projects too.http://ltwiki.org/index.php5?title=B_Arbitrary_behavioral_voltage_or_current_sources

author: Ian.M
author_rank: Super Contributor
timestamp: August 30, 2017, 01:04:18 pm
content: If you are looking for something that can add a realtime frequency (as a voltage) trace to your .tran plot, there'sFreqDet- available as part of a zipped collection fromhttp://ltspicegoodies.ltwiki.org/index.html

author: StillTrying
author_rank: Super Contributor
timestamp: August 30, 2017, 01:47:30 pm
content: I'm afraid I zoom by dragging a box over the waveform, when zoomed to just 3 or 4 cycles across the screen I drag a box again across exactly 1 cycle, whileholdingthe box width at 1 cycle the frequency is shown in the status bar at the bottomright.left.If you make the second box verynarrowsmallbeforeletting go of it, it doesn't zoom.Right-Clicking over the plot gives you "Zoom to Fit", which zooms out.

author: WaveyDipole
author_rank: Frequent Contributor
timestamp: August 30, 2017, 02:37:20 pm
content: Quote from: AndyC_772 on August 30, 2017, 12:06:33 pmThanks. I noticed that it seems and maybe more accurate to measure the cycle from the mid-point. Thanks for this tip.Quote from: Ian.M on August 30, 2017, 11:34:47 amIan, the point is well taken. I thought my question 'newbie' enough to not have to worry about that this time, but I will bear it in mind if I need to ask anything in the future. Thanks also for posting those examples of spice directives to measure frequency. I am going to have a play with this in a while and will report back later.Quote from: StillTrying on August 30, 2017, 01:47:30 pmTime for me to post another! For quick measurements what you say here is so much easier and quicker than messing with the scroll wheel etc. Thanks for pointing it out.UPDATE: I was able to take measurements using spice directives as shown in the examples from Ian.M. I used the Tstart option combined with the average of 10 rises example. This initially worked OK provided that I was attempting to do only one measurement. When I tried to set up a second set of spice directives for another frequency measurement I started running into errors telling me that each .measure needed a unique result name. I eventually found some information in the LTspice WiKi about the format of the .measure statement and did something like this which worked:Code:.meas tran LoTs Tstart.meas tran LoT1 find time when V(losc)=0 rise 1.meas tran LoT2 find time when V(losc)=0 rise 11.meas tran LoFq param 10/(LoT2-LoT1).meas tran RfTs Tstart.meas tran RfT1 find time when V(osig)=0 rise 1.meas tran RfT2 find time when V(osig)=0 rise 11.meas tran RfFq param 10/(RfT2-RfT1)This seems to work fine for repetitive measurements and a quick review of the log file is all that is needed after each iteration. Thanks again for those examples.