現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
Real time audio data plot from COM/serial port
2 ビュー (過去 30 日間)
古いコメントを表示
Kulbir
2023 年 4 月 7 日
need to know how i can plot audio data in real-time from serial port. am getting error using this code.
clear; clc;
%create a serial object
s= serialport("COM4", 115200);
%open serial port
fopen(s);
%plotting data
i= 1;
while(1)
data(i)= str2double(fscanf(s));
plot(data);
pause(0.01);
i= i+1;
end
%error i am getting is---
Error using fopen
First input must be a file name or a file identifier.
Error in pltsril (line 5)
fopen(s);
回答 (1 件)
Cris LaPierre
2023 年 4 月 7 日
fopen does not work with a serialport object. See the available methods here:
27 件のコメント
Walter Roberson
2023 年 4 月 8 日
編集済み: Walter Roberson
2023 年 4 月 8 日
Or sscanf(readline(s)) if the information is formatted as text.
Also consider using animatedline
Kulbir
2023 年 4 月 10 日
hello sir, i am not able to plot/animate real time audio signal, would b so helpful if you share the code for that. thank you
Kulbir
2023 年 4 月 10 日
編集済み: Kulbir
2023 年 4 月 11 日
Sir here is the code which i am being using.
device = serialport("COM3",9600)
write(device,1:5,"uint8")
read(device,5,"uint8")
while(1)% infinite loop
i=1; %not able to see the progress line
data(i)= str2double(fscanf(device));
plot(data);
pause(0.01);
i= i+1;
end
Walter Roberson
2023 年 4 月 11 日
device = serialport("COM3",9600)
write(device,1:5,"uint8");
read(device,5,"uint8");
h = animatedline('MaximumNumPoints', 10000);
start = tic;
while(1)
data = str2double(fscanf(device));
now = toc(start);
addpoints(h, now, data);
drawnow limitrate
end
Kulbir
2023 年 4 月 13 日
hi sir, the program/code is running correctly, but am not able to see plotting in the graph. As you asked how i am reading the audio data. Actually am using pressure transducer and pneumotacometer to sense vocal track aerodynamics. i have made usb operated DAC circuit. kindly help me in resolving the issue. thank you

Cris LaPierre
2023 年 4 月 13 日
Your workspace is conspicuously empty. I would expect to see device, data, now and other variables created by your script appear there.
Kulbir
2023 年 4 月 13 日
hi sir , i can see the device data in the workspace, when i press stop the script. but not able to see the plot in graph. plz help me out

Cris LaPierre
2023 年 4 月 13 日
編集済み: Cris LaPierre
2023 年 4 月 13 日
The error message is stating that you are not connected to the serial port device.
I suggest adding the following to the bottom of your script to clear your serialport object each time.
clear device
I would also suggest not using fscanf with a serialport object. Try this.
data = str2double(readline(device));
Walter Roberson
2023 年 4 月 13 日
Cris beat me by a small number of seconds in pointing out you need readline().
Kulbir
2023 年 4 月 13 日
Hi sir(s) thx for your help... unfortunately am not getting anything in the grapgh, have tried either ways. kindly help me out. as i said in my input/com device there is no microphone. its sensor/transducer and pneumotacometer as i mentioned in the trailing message. thank you
Walter Roberson
2023 年 4 月 13 日
Did you possibly close the drawing figure?
Your code has no natural way to stop. If you tried to stop by closing the figure then you would get exactly the message you saw about needing a handle.
Cris LaPierre
2023 年 4 月 13 日
I'll add the observation that the result of your read command appears to have been successful (variable ans in the Workspace has values), but data is NaN. Are you sure your device is streaming anything?
Kulbir
2023 年 4 月 13 日
Hi, when i run the following script, i dont see the device data in workspace. but if i close the drawing graph, the i am able to see the data in workspace. device = serialport("COM4",9600)
write(device,1:5,"uint8");
read(device,5,"uint8");
h = animatedline('MaximumNumPoints', 10000);
start = tic;
while(1)
data = str2double(readline(device));
now = toc(start);
addpoints(h, now, data);
drawnow limitrate
end
Kulbir
2023 年 4 月 13 日
yes, sir, actually i am trying to capture vocal track aerodynamic signals. i have done it in c sharp. used the same animate line there. it is working in c sharp, but here i am not able to run suceesfully. my ultimate goal is to plot real time audio and vocal track pressure. thk u for helping me.
Walter Roberson
2023 年 4 月 13 日
For testing purposes, please try this:
device = serialport("COM4",9600)
write(device,1:5,"uint8");
first_5 = read(device,5,"uint8");
fprintf('Not sure why we are reading 5 bytes, but the values received were:\n');
disp(first_5);
h = animatedline('MaximumNumPoints', 10000);
start = tic;
while isvalid(h) && toc(start) < 30 %FOR TESTING
currentline = readline(device);
data = str2double(currentline);
if isnan(data)
fprintf('data nan, line as text was: "%s"\n', currentline);
continue;
end
now = toc(start);
if isvalid(h)
addpoints(h, now, data);
drawnow limitrate
end
end
At this point I would not be surprised if it turned out that each line of data received is text with multiple numeric values.
Kulbir
2023 年 4 月 13 日
yes sir, can see it in the matlab, plz check the trailing communication. thank you
Kulbir
2023 年 4 月 13 日
hi sir(s), i would like to say, there is a knob/button on my device to collect the data, but i am geeting this data without pressing the button
Walter Roberson
2023 年 4 月 13 日
So far, except for the first 5 bytes, your code has been as-if the data being sent from the other side is text. For example your existing code is expecting to receive lines similar to
0.15180
-0.3351
as text .
But the other end is not sending text, it is sending binary values. Instead of using readline() and str2double(), you need to use read . But to use that successfully, you need to know what format the other end is sending the samples. The most common data formats would correspond to
read(device, 1, 'uint8') %8 bit unsigned data
read(device, 1, 'uint16') %16 bit unsigned data, little-endian
read(device, 1, 'float') %32 bit single precision, little-endian
swapbytes(read(device, 1, 'uint16')) %16 bit unsigned data, big-endian
swapbytes(read(device, 1, 'float')) %32 bit single precision, big-endian
but there are other possibilities as well.
For example, it would not be uncommon to send packed 12-bit data, 2 samples stored across 3 bytes.
Kulbir
2023 年 4 月 13 日
sir tried with the new script, getting the data in different way, less coulmns. thank you for helping me. 

Kulbir
2023 年 4 月 13 日
sir there are two channels in my device. but i think that is not the part of our discussion. i am expecting plotting of audio signal firsty. thank you
Walter Roberson
2023 年 4 月 13 日
Using readline(device) is not going to be useful for you. You need to find out what data format the device is sending, and you need to use read() of the device, probably using one of the commands I showed above.
What model of device are you reading from?
参考
カテゴリ
Help Center および File Exchange で Multirate Signal Processing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)



