⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 daqwaterfall.m

📁 单片机与PC机之间的无线传输
💻 M
📖 第 1 页 / 共 2 页
字号:
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ++++ C H A N G E   T H E   S A M P L E   R A T E  ++++
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% --- Executes on selection change in poSampleRate.
function poSampleRate_Callback(hObject, eventdata, handles)

% hObject    handle to poSampleRate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns poSampleRate contents as cell array
%        contents{get(hObject,'Value')} returns selected item from poSampleRate

% First, stop and delete the current analog input object
localStopAI(handles.ai);

% Extract the new samplerate.
v=get(handles.poSampleRate,'Value');
s=get(handles.poSampleRate,'String');
handles.sampleRate = str2num(s{v});

% Create a new analog input with the new sample rate.
handles.ai = localSetupAI(handles);

% Update handles structure
guidata(hObject, handles);

% Restart the analog input
localStartAI(handles.ai);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ---- C H A N G E   T H E   S A M P L E   R A T E  ----
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



% ***********************************************************************  
% Calculate the fft of the data.  (Copied from demoai_fft.m)
function [f, mag] = localDaqfft(data,Fs,blockSize)

% Calculate the fft of the data.
xFFT = fft(data);
xfft = abs(xFFT);

% Avoid taking the log of 0.
index = find(xfft == 0);
xfft(index) = 1e-17;

mag = 20*log10(xfft);
mag = mag(1:blockSize/2);

f = (0:length(mag)-1)*Fs/blockSize;
f = f(:);


% ***********************************************************************  
% Update the plot. This routine is a Timer callback, it is called
%  automatically at a preset time interval. See line 184 for where
%  this routine is assigned as a callback
function localfftShowData(obj,event)

if (get(obj,'SamplesAvailable') >= obj.SamplesPerTrigger)
	
	% Get the handles.
	data = obj.UserData;
	
	handles = data.figureHandles;
	
	% Execute a peekdata.
	x = peekdata(obj, obj.SamplesPerTrigger);
	
	% FFT calculation.
	Fs = obj.SampleRate;
	blockSize = obj.SamplesPerTrigger;
	[f,mag] = localDaqfft(x,Fs,blockSize);
	
	% Dynamically modify Analog axis as we go.
	maxX=max(x);
	minX=min(x);
	yax1=get(handles.axes1,'YLim');
% 	yax1(1)=minX - .0001; % need to subtract a value to make sure yax(1) never equals yax(2)
%     yax1(2)=maxX + .0001; 
    if minX<yax1(1),
        yax1(1)=minX;
    end
    if maxX>yax1(2),
        yax1(2)=maxX;
    end
	set(handles.axes1,'YLim',yax1)
%	set(handles.axes1,'XLim',[0 (obj.SamplesPerTrigger-1)/obj.SampleRate])
	set(handles.axes1,'XLim',[0 (obj.SamplesPerTrigger-1)/obj.SampleRate])
        
	% Dynamically modify Frequency axis as we go.
	maxF=max(f);
	minF=min(f);
	xax=get(handles.axes2,'XLim');
    xax(1)=minF;
    xax(2)=maxF;
	set(handles.axes2,'XLim',xax)
    %set(handles.axes2,'XLim',[0 8000])
	
	% Dynamically modify Magnitude axis as we go.
	maxM=max(mag);
    minM=min(mag);
    yax2=get(handles.axes2,'YLim');
    % 	yax2(1)=minM - .0001;
    % 	yax2(2)=maxM + .0001;
    if minM<yax2(1),
        yax2(1)=minM;
    end
    if maxM>yax2(2),
        yax2(2)=maxM;
    end
    set(handles.axes2,'YLim',yax2)
    %set(handles.axes2,'XLim',[0 8000]);
    
	% Update the line plots
	set(handles.hLine1, 'XData', [0:(obj.SamplesPerTrigger-1)]/obj.SampleRate, 'YData', .5+15*x(:,1));
	set(handles.hLine2, 'XData', f(:,1), 'YData', mag(:,1));

    % Find the frequency at which the max signal strength is at.
    [ymax,maxindex] = max(mag);
    set(handles.tFreq,'String',sprintf('%4.1d Hz',f(maxindex)));
    
    % Store the current FFT into the array of FFTs used for the waterfall.
	data.storedFFTs(data.storedFFTsIndex,:) = mag';
	
    % This circular shift is used so that when we display the 3D plot, the 
    %  newest FFT will appear in 'front' and the oldest in 'back'. 
    % To understand this, note how the plotting routines are using this fftOrder 
    %  array to reorder the FFTs stored in data.storedFFTs and also note
    %  how data.storedFFTsIndex is used to store FFTs in data.storedFFTs.
    %
	fftOrder = 1:handles.numTraces;
	fftOrder = circshift(fftOrder,[ 1 -data.storedFFTsIndex ]);  
	
	data.storedFFTsIndex = data.storedFFTsIndex + 1;
	if (data.storedFFTsIndex > handles.numTraces)
        data.storedFFTsIndex = 1;
        data.plotSurf        = 1; % Indicates a full history is stored.
	end
	
	% Update the surface plot if we have a full history.
	if (data.plotSurf)
        cla(handles.axes3);

        v=get(handles.poPlotType,'Value');
        s=get(handles.poPlotType,'String');
        switch s{v}
            case 'Classic'
                data.view  = [103 30];
                data=localClassic(handles,data,f,fftOrder);
            case 'Classic(Top)'
                data.view  = [90 -90];
                data=localClassic(handles,data,f,fftOrder);
            case 'Mosaic'
                data.view  = [90 -90];
                data=localMosaic(handles,data,f,fftOrder);
            case 'Waterfall'
                data.view = [103 30];
                data=localWaterfall(handles,data,f,fftOrder,yax2);
            case 'Rotate'
                data=localRotate(handles,data,f,fftOrder);
            case 'CycleAll'
                data=localCycleAll(handles,data,f,fftOrder);
        end
                
    end
	
	set(data.ai, 'UserData', data);
	
	drawnow;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ++++ V I S U A L I Z A T I O N ++++
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function data=localClassic(handles,data,f,fftOrder)
    [X,Y] = meshgrid(1:handles.numTraces,f(1:end));
    surf(X,Y,data.storedFFTs(fftOrder,:)','parent',handles.axes3);                
  	set(handles.axes3,'XLim',[1 handles.numTraces],'YLim',[0 f(end)])
    shading(handles.axes3,'interp');
    set(handles.axes3,'View',data.view)

function data=localMosaic(handles,data,f,fftOrder)
    [X,Y] = meshgrid(1:handles.numTraces,f(1:10:end));
    surf(X,Y,data.storedFFTs(fftOrder,(1:10:end))','parent',handles.axes3);
  	set(handles.axes3,'XLim',[1 handles.numTraces],'YLim',[0 f(end)])
    set(handles.axes3,'View',data.view)
       
function data=localWaterfall(handles,data,f,fftOrder,yax2)
    [X,Y] = meshgrid(1:handles.numTraces,f(1:end));
    p=plot3(X,Y,data.storedFFTs(fftOrder,:)','parent',handles.axes3);                

    % rotate the color map of the lines in the plot3
    map  = linspace(0,1,handles.numTraces);
    map2 = linspace(1,0,handles.numTraces);
    rotatemap  = map(fftOrder);
    rotatemap2 = map2(fftOrder);
    for k=1:handles.numTraces;
        set(p(k),'Color',[rotatemap(k) .1 rotatemap2(k)]);
    end
    
  	set(handles.axes3,'XLim',[1 handles.numTraces],'YLim',[0 f(end)]);
    shading(handles.axes3,'interp');
    set(handles.axes3,'View',data.view)

function data=localRotate(handles,data,f,fftOrder)
    [X,Y] = meshgrid(1:handles.numTraces,f(1:8:end));
    surf(X,Y,data.storedFFTs(fftOrder,(1:8:end))','parent',handles.axes3);
  	set(handles.axes3,'XLim',[1 handles.numTraces],'YLim',[0 f(end)])
    set(handles.axes3,'View',data.view)
    
    % Rotate the view point.
    data.view(1) = 90;
    if data.view(2) >= 90-data.rotateStep
        data.rotateStep = -4;
    elseif data.view(2) <= -90-data.rotateStep
        data.rotateStep = 4;
    end    
    data.view(2) = data.view(2)+data.rotateStep;    

function data=localCycleAll(handles,data,f,fftOrder)
    data.counter = data.counter + get(data.ai,'TimerPeriod');
    if data.counter > 5*handles.cycleTime
        data.counter = 0;
    elseif data.counter > 4*handles.cycleTime
        data=localRotate(handles,data,f,fftOrder);
    elseif data.counter > 3*handles.cycleTime
        data.view  = [103 30];
        data=localWaterfall(handles,data,f,fftOrder);
    elseif data.counter > 2*handles.cycleTime
        data.view  = [90 -90];
        data=localMosaic(handles,data,f,fftOrder);
    elseif data.counter > 1*handles.cycleTime
        data.view  = [90 -90];
        data=localClassic(handles,data,f,fftOrder);
    else
        data.view  = [103 30];
        data=localClassic(handles,data,f,fftOrder);
    end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ---- V I S U A L I Z A T I O N ----
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
% --- Executes during object creation, after setting all properties.
function poPlotType_CreateFcn(hObject, eventdata, handles)
% hObject    handle to poPlotType (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on selection change in poPlotType.
function poPlotType_Callback(hObject, eventdata, handles)
% hObject    handle to poPlotType (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns poPlotType contents as cell array
%        contents{get(hObject,'Value')} returns selected item from poPlotType


% --- Executes on button press in StartStopButton.
function StartStopButton_Callback(hObject, eventdata, handles)
% hObject    handle to StartStopButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of StartStopButton

%% This code is not needed.
% daqstopbutton wires the button automatically

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -