Emg signalini raqamli qayta ishlash dasturi
Download 96.52 Kb.
|
DGU uchun
- Bu sahifa navigatsiya:
- EHM uchun dasturni identifikatsiya qiluvchi materiallar dastlabki matni (Dastur kodi)
Deponentlanadigan materiallarning titul varag‘i EHM uchun dastur (Ma’lumotlar bazasi) nomi: “EMG signalini raqamli qayta ishlash dasturi” Huquq ega(lar)si : Zohirov Qudratjon Rafiqovich Muallif(lar): Zohirov Adhamjon Rafiqovich EHM uchun dasturni identifikatsiya qiluvchi materiallar dastlabki matni (Dastur kodi) fname=uigetfile({'*.txt','Teкстовый файл'}); fid=fopen(fname,'rt'); classdef Bitalino < handle properties SampleRate end properties (SetAccess = private) AvailableSamples = 0 FirmwareVersion; properties (Hidden = true) VerifyCRC = false; deviceTimeout = 0.2; end properties (Access = private) BTObject; RawFrames; isStreaming = false; numBytesPerRead; D0Value; end methods (Static) %% Find all Bluetooth devices function availableDevices = findDevices() availableBTDevices = instrhwinfo('Bluetooth'); availableBTDeviceNames = lower(availableBTDevices.RemoteNames); inds = strfind(availableBTDeviceNames,'bitalino'); inds = not(cellfun(@isempty,inds)); availableDevices = availableBTDevices.RemoteIDs(inds); end end methods function obj=Bitalino(varargin) if nargin>2 error('bitalino:invalidInputs','Invalid number of input arguments. See ''help bitalino'' for more information.'); end try if (nargin == 0) obj.BTObject=Bluetooth('bitalino',1); else validateattributes(varargin{1},{'char'},{'vector'}) obj.BTObject=Bluetooth(varargin{1},1); end set(obj.BTObject, 'Tag','Bitalino'); set(obj.BTObject, 'InputBufferSize',2^16); set(obj.BTObject, 'Terminator',''); set(obj.BTObject, 'ByteOrder', 'bigEndian'); set(obj.BTObject, 'ObjectVisibility','off'); fopen(obj.BTObject); catch myException throw(myException); end pause(obj.deviceTimeout); obj.FirmwareVersion = char(fread(obj.BTObject,obj.BTObject.BytesAvailable))'; flushinput(obj.BTObject); % Set sampling rate if (nargin==2) validateattributes(varargin{2},{'numeric'},{'scalar'}); obj.SampleRate = varargin{2}; else obj.SampleRate = 1000; end writeDigitalPins(obj,{'D0','D1','D2','D3'},{0,0,0,0}); end n = size(obj.RawFrames,1); data = obj.decodeFrames(obj.RawFrames(1:n,:)); if size(obj.RawFrames,1)>n obj.RawFrames = obj.RawFrames(n+1:end,:); else obj.RawFrames = []; end end function data = readCurrentValues(obj, varargin) if nargin>2 throw(MException('bitalino:readInstantaneous:invalidInputs','Invalid number of input arguments. READINSTANTANEOUS only takes at most 2 inputs')); elseif (nargin==2) validateattributes(varargin{1},{'numeric'},{'scalar'}); n = varargin{1}; else n = 1; end if isequal(getStreamingState(obj),true) data = obj.RawFrames(end-(n-1):end,:); else cachedArray = obj.RawFrames; cachedSampleRate = obj.SampleRate; obj.RawFrames = []; obj.SampleRate = 1000; obj.enableBackgroundProcessing; while (size(obj.RawFrames,1) end obj.disableBackgroundProcessing; flushinput(obj.BTObject); drawnow; data = decodeFrames(obj,obj.RawFrames(end-(n-1):end,:)); obj.RawFrames = cachedArray; obj.SampleRate = cachedSampleRate; end end function startBackground(obj) if isequal(getStreamingState(obj),false) obj.RawFrames = []; enableBackgroundProcessing(obj); obj.isStreaming = true; else throw(MException('bitalino:startBackground:isStreaming','This operation is only allowed when the bitalino is not streaming data')); end end function stopBackground(obj) if isequal(getStreamingState(obj),true) disableBackgroundProcessing(obj); obj.isStreaming = false; else throw(MException('bitalino:stopBackground:notStreaming','This operation is only allowed when the bitalino is streaming data')); end end function set.SampleRate(obj, value) % % b.SampleRate = 100 if isequal(getStreamingState(obj),true) throw(MException('bitalino:sampleRate:cannotChangeWhenStreaming','Cannot change the sample rate when streaming. To change the sample rate, STOP the object first.')); end switch vlue case 1000 valueToWrite = bitor(3,bitshift(3,6)); setNumBytesPerRead(obj,800); case 100 valueToWrite = bitor(3,bitshift(2,6)); setNumBytesPerRead(obj,200); case 10 valueToWrite = bitor(3,bitshift(1,6)); setNumBytesPerRead(obj,40); case 1 valueToWrite = bitor(3,bitshift(0,6)); setNumBytesPerRead(obj,8); otherwise throw(MException('bitalino:sampleRate:invalidSampleRate','Invalid sample rate specified. Valid rates are 1, 10, 100, 1000 Hz')); end write(obj,valueToWrite); obj.SampleRate = value; end if (numel(unique(pins)) ~= numel(pins)) throw(MException('bitalino:writeDigitalPins:invalidInputs','The value for a digital output pin is specified more than once in the list of pins. Ensure the list only contains a unique value for each output pin')); end if (numel(pins) ~= numel(values)) throw(MException('bitalino:writeDigitalPins:invalidInputs','The specified number of digital pins does not match the specified number of output values')); end for i = 1:numel(pins) switch lower(pins{i}) case 'd0' if values{i} obj.D0Value = 1; else obj.D0Value = 0; end case 'd1' if values{i} obj.D1Value = 1; else obj.D1Value = 0; end case 'd2' if values{i} obj.D2Value = 1; else obj.D2Value = 0; end case 'd3' if values{i} obj.D3Value = 1; else obj.D3Value = 0; end otherwise throw(MException('bitalino:writeDigitalPins:invalidOutputPin','Invalid digital output pin specified. Valid values are ''D0'',''D1'',''D2'' and ''D3''')); end end valueToWrite = bitshift(obj.D0Value,2,'uint8') + ... bitshift(obj.D1Value,3,'uint8') + ... bitshift(obj.D2Value,4,'uint8') + ... bitshift(obj.D3Value,5,'uint8'); if (obj.VerifyCRC) % Extract CRC CRC = bitand(data(i,8),15,'uint8'); % 15 = bin2dec('00001111') data(i,8) = bitand(data(i,8),240,'uint8'); % 240 = bin2dec('11110000') % Decode the Cyclic Redundancy Check and cross verify data(i,8) = bitand(data(i,8),240); calcCRC = 0; for j = 1:1:8 for bit = 7:-1:0 calcCRC = bitshift(calcCRC,1,'uint8'); if bitand(calcCRC,16) calcCRC = bitxor(calcCRC,3,'uint8'); end calcCRC = bitxor(calcCRC, ... bitand(1, bitshift(data(i,j),-1*bit)),'uint8'); end end calcCRC = bitand(uint8(calcCRC),15,'uint8'); if ~isequal(CRC,calcCRC) throw(MException('bitalino:read:CRCError',sprintf('Error when decoding frame. CRC = %d, calculated CRC = %d',CRC, calcCRC))); end end sendToIdle(obj); fclose(obj.BTObject); % Flush data in Bluetooth buffer and MATLAB EDT flushinput(obj.BTObject); drawnow; set(obj.BTObject, 'BytesAvailableFcn','', ... 'BytesAvailableFcnMode', 'byte', ... 'BytesAvailableFcnCount', obj.numBytesPerRead); fopen(obj.BTObject); end %% Enable the Bluetooth object's callback for background processing function enableBackgroundProcessing(obj) % Send Bitalino into live mode and stream data from the device fclose(obj.BTObject); % Flush data in Bluetooth buffer and MATLAB EDT flushinput(obj.BTObject); set(obj.BTObject, 'BytesAvailableFcn',@(handle,object)obj.readFrame, ... 'BytesAvailableFcnMode', 'byte', ... 'BytesAvailableFcnCount', obj.numBytesPerRead); drawnow; fopen(obj.BTObject); pause(obj.deviceTimeout); sendToLive(obj); end %% Return BITalino streaming status function Value = getStreamingState(obj) % 1 = object is streaming % 0 = object is not streaming Value = obj.isStreaming; end %% Read data frame for BITalino object function readFrame(obj,~) % Receive data frame from the bitalino device on callback % Read bytes of data from bitalino data=fread(obj.BTObject,obj.numBytesPerRead,'uchar'); obj.RawFrames = [obj.RawFrames; reshape(data,[8,floor(obj.numBytesPerRead/8)])']; end %% Turn on idle mode function obj=sendToIdle(obj) write(obj,0); end %% Turn on live mode function obj=sendToLive(obj) write(obj,253); end %% Set the internal property numBytesPerRead function setNumBytesPerRead(obj,Value) obj.numBytesPerRead = Value; end function obj=write(obj, data) drawnow; fwrite(obj.BTObject,data,'uint8'); pause(obj.deviceTimeout); end end end y=fscanf(fid,'%f',[1,Inf]); figure(1) fd = 1600; T=1/fd; t= 0:T:(length(y)-1)*T; plot(t,y) title('Исходный сигнал') xlabel('Время (t)'); Y = fft(y); PSDPyy = abs(Y).^2; f = -fd/2:fd/(length(PSDPyy)-1):fd/2; f=fftshift(f); figure(2) plot(f(1:fix(end/2)),PSDPyy(1:fix(end/2))) title xlabel('Частота Гц'); fres=f(2)-f(1); % n5=fix(5/fres+1);% n150=fix(150/fres+1); n300=fix(300/fres+1); n800=fix(800/fres+1); SVLF=trapz(f(n5:n150),PSDPyy(n5:n150)) SVLF=trapz(f(n150:n300),PSDPyy(n150:n300)) SVLF=trapz(f(n300:n800),PSDPyy(n300:n800)) Download 96.52 Kb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling