% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
isrecording=getappdata(gcf,'isrecording');
if ~isrecording
setappdata(gcf,'isrecording',1);
recorder(gcf,handles);
else
setappdata(gcf,'isrecording',0);
end
附上record函数的代码:(record函数的作用是建立声卡对象,控制声卡实现录音,并对录音的数据进行实时的时域和频域显示)
function aa= recorder(cf,handles)
%RECORDER Summary of this function goes here
% Detailed explanation goes here
% h=figure(soundrec);
h=cf;
thehandles=handles;
setappdata(h,'isrecording',1);
Ai=analoginput('winsound'); % 创建一个模拟信号输入对象
% 添加通道
addchannel(Ai,1);
Ai.SampleRate=10000; % 采样频率
Ai.SamplesPerTrigger=Inf; % 采样数
start(Ai); % 开启采样
warning off % 当采样数据不够时,取消警告
while isrunning(Ai) % 检查对象是否仍在运行
if getappdata(h,'isrecording')
data=peekdata(Ai,Ai.SampleRate/2);% 获取对象中的最后Ai.SampleRate个采样数据
plot(thehandles.axes1,data) % 绘制最后Ai.SampleRate个采样数据的图形,因此表现出来就是实时的了
set(handles.axes1,'ylim',[-1 1],'xlim',[0 5000]);
y1=fft(data,2048); %对信号做2048点FFT变换
f=Ai.SampleRate*(0:1023)/2048;
bar(handles.axes2,f,abs(y1(1:1024)),0.8,'g') %做原始语音信号的FFT频谱图
set(handles.axes2,'ylim',[0 10],'xlim',[100 250]);%设置handles.axes2的横纵坐标范围
drawnow; % 刷新图像
else
stop(Ai);
num=get(Ai,'SamplesAvailable');
aa=getdata(Ai,num);
axes(thehandles.axes1);
plot(thehandles.axes1,aa) % 绘制所有采样数据的图形
y1=fft(data,2048); %对信号做2048点FFT变换
f=Ai.SampleRate*(0:1023)/2048;
bar(handles.axes2,f,abs(y1(1:1024)),0.8,'g') %做原始语音信号的FFT频谱图
drawnow; % 刷新图像
setappdata(h,'sounds',aa);
end
end
end
播放按钮的功能是:按下时,播放已经录入的音频,并实时显示频谱,这就需要自己设计记录音频播放的位置,这样才能使在循环内每次音频的播放位置正确,记录位置通过计时来实现,记录的时间差time转换为以秒为单位得到s,再通过s*fs来得到采样了多少个点,从而得到采样点的位置,并且限制每次处理5000个采样点,在时域波形上刷新显示;而频域的计算则是每次取2048个采样点进行fft变换。
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global ad fs yp y ;
y=getappdata(gcf,'sounds');
fs=10000;
ad=analogoutput('winsound');%建立以声卡作为输入输出对象
addchannel(ad,1);
set(ad,'samplerate',fs);
putdata(ad,y);
handles.ad=ad;
guidata(hObject, handles);%更新图形句柄
start(ad);
T=clock;
pause(0.1);
while isrunning(ad)
T1=clock;
time=T1-T;%计算时间差
s=3600*time(4)+60*time(5)+time(6);
if(round(s*fs+5000)
yp=y(round(s*fs):round(s*fs+5000));%round函数取距离最近的整数
plot(handles.axes1,yp);
set(handles.axes1,'ylim',[-1 1],'xlim',[0 5000]);
y1=fft(yp,2048);
f=fs*(0:1023)/2048;
bar(handles.axes2,f,abs(y1(1:1024)),0.8,'g') %做原始语音信号的FFT频谱图
set(handles.axes2,'ylim',[0 10],'xlim',[100 250]);%设置handles.axes2的横纵坐标范围
drawnow;
end
end
程序不对,请讲解一下