从 ADC 获取数据时如何测量 python 中的采样率?
How to measure the sample rate in python when getting data from a ADC?
我在一个使用 Raspberry Pi 3 B 的项目中工作,我通过 ADC MPC3008 从红外传感器(夏普 GP2Y0A21YK0F)获取数据,并使用 PyQtgraph 库实时显示它。
但是,我得到的样本似乎很少,图表也不是我预期的 "smooth"。
我正在使用 Adafruit Python MCP3008 库和函数 mcp.read_adc(0)
来获取数据。
有没有办法测量 Python 中的采样率?
谢谢
雨果·奥利维拉
我建议设置一些下一级缓冲,最好是通过多处理(参见 multiprocessing and GUI updating - Qprocess or multiprocessing?) to better get a handle on how fast you can access the data. Currently you're using a QTimer to poll with, which is only getting 3 raw reads every 50 msec... so you're REALLY limiting yourself artificially via the timer. I haven't used the MCP3008, but a quick look at some at their code seems like you'll have to set up some sample testing to try some things out, or investigate further for better documentation. The question is the behavior of the mcp.read_adc(0) method and is it blocking or non-blocking... if non-blocking, does it return stale data if there's no new data, ... etc. It would be ideal if it was blocking from a timing sense, you could just set up a loop on it and time delta each successive return to determine how fast you're able to get new samples. If it's non-blocking, you would want it to return null for no new samples, and only return the actual samples that were new if it does return something. You'll have to play around with it and see how it behaves. At any rate, once you get the secondary thread set up to just poll the mcp.read_adc(0), then you can use the update() timer to collect the latest buffer and plot it. I also don't know the implications of multi-threading / multiprocessing on the RaspPI (see general discussion here: Multiprocessing vs Threading Python),但任何东西都应该比 QTimer 轮询更好。
我在一个使用 Raspberry Pi 3 B 的项目中工作,我通过 ADC MPC3008 从红外传感器(夏普 GP2Y0A21YK0F)获取数据,并使用 PyQtgraph 库实时显示它。
但是,我得到的样本似乎很少,图表也不是我预期的 "smooth"。
我正在使用 Adafruit Python MCP3008 库和函数 mcp.read_adc(0)
来获取数据。
有没有办法测量 Python 中的采样率?
谢谢
雨果·奥利维拉
我建议设置一些下一级缓冲,最好是通过多处理(参见 multiprocessing and GUI updating - Qprocess or multiprocessing?) to better get a handle on how fast you can access the data. Currently you're using a QTimer to poll with, which is only getting 3 raw reads every 50 msec... so you're REALLY limiting yourself artificially via the timer. I haven't used the MCP3008, but a quick look at some at their code seems like you'll have to set up some sample testing to try some things out, or investigate further for better documentation. The question is the behavior of the mcp.read_adc(0) method and is it blocking or non-blocking... if non-blocking, does it return stale data if there's no new data, ... etc. It would be ideal if it was blocking from a timing sense, you could just set up a loop on it and time delta each successive return to determine how fast you're able to get new samples. If it's non-blocking, you would want it to return null for no new samples, and only return the actual samples that were new if it does return something. You'll have to play around with it and see how it behaves. At any rate, once you get the secondary thread set up to just poll the mcp.read_adc(0), then you can use the update() timer to collect the latest buffer and plot it. I also don't know the implications of multi-threading / multiprocessing on the RaspPI (see general discussion here: Multiprocessing vs Threading Python),但任何东西都应该比 QTimer 轮询更好。