power spectral density coding in Matlab on my EEG signal? - matlab

Okay, I need to apply PSD on my EEG signal.. My current program code is below where y_theta is my data name.
The problem is, I was asked by my lecturer to create another plot using PSD but without changing the amplitude into dB/Hz (which is the y axis)..How can I do that?
length(y_theta)
nfft = 2^nextpow2(length(y_theta));
Fs=128;
Pxx = abs(fft(y_theta,nfft)).^2/length(y_theta)/Fs;
subplot(2,2,2),
plot(Pxx,'DisplayName','Pxx','YDataSource','Pxx');
Hpsd = dspdata.psd(Pxx(1:length(Pxx)/2),'Fs',Fs);
plot(Hpsd);

Related

How to draw a Fourier spectra for my txt file data on MatLab? [duplicate]

This question already has answers here:
Understanding Matlab FFT example
(4 answers)
Closed 7 years ago.
I have a ground-based magnetic data in txt file taken every second and I want to plot its Fourier Spectra.
And I notice the spectrogram equation on MatLab need this parameters (window, noverlap, nfft, Fs) which I don't know.
And I notice also that I should do the FFT on my data first before plot its spectrogram, but my data is not discrete and FFt for the discrete data, anyone know how I can do this?
data is discrete by definition. spectrogram and fft give different view s of the data.
spectrogram would be appropriate for a STFT short-time Fourier transform, in case you want to look at successive, overlapping windows of time. fft is the method spectrogram will use to compute the transform.
. Fs is the sampling rate, which you say is once per second, so 1 Hz. Here is one way to view the spectrum,
Fs = 1;
X = fft(data);
N = length(data);
freq = (-N/2:N/2 - 1)*Fs/N;
XmagdB = 10*log10(X.*conj(X));
plot(freq, XmagdB)
Good luck!

Find transfer function from bode plot in matlab

I have obtained the bode plot for a system. The system seems to have a very complex magnitude and phase plot. It's not possible to find the transfer function manually. Is there a way of finding the transfer function from the magnitude and phase data, in Matlab?
Here's my code:
%%FFT method for finding Transfer Function
load testdata2.mat;
input = fft(signal(:,1));
% FFT of input data
output = fft(signal(:,2));
% FFT of output data
fft_ratio = output ./ input;
subplot(2,1,1)
%Magnitude
semilogx(20*log10(abs(fft_ratio)))
subplot(2,1,2)
%Phase
semilogx((180/pi)*angle(fft_ratio))
mag = 20*log10(abs(fft_ratio));
phase = (180/pi)*angle(fft_ratio);
Here's my data.
I don't believe so, and that's not Matlab's fault. The problem is mathematically nontrivial because there can be poles and zeros of the transfer function that lie at large imaginary frequency. These might not significantly affect the Bode plot, but how would you rule out their existence?
I think your best bet is to fit the Bode plot to a rational transfer function, and just keep increasing the number of poles and zeros in the transfer function until you get acceptable agreement.

Analyzing wav files in MATLAB

So I have this piano recording (in .wav format). I am able to do an FFT on the whole recording and identify frequencies.
However, according to some articles I read, its best if the wav file is broken down into windows, where each window would include one particular note.
For this I need to initially plot a "power envelope" of my time domain signal (considering the note average energy concept) therefore there'll be one increase and one decrease for each note and note onsets can be determined by checking the local minima.
This is where 'windows' are introduced, where each window consists of only one onset and then FFT is performed on each window.
Im having difficulty in plotting the power envelope and moving onto breaking it down into windows. Would appreciate some help with the Matlab coding for this.
The code I've used is pretty straightforward:
[wave,fs] = wavread ('c scale fast.wav'); % read file into memory */
%sound(wave,fs); % see what it sounds like */
wave = wave.*hamming(length(wave));
t = 0:1/fs:(length(wave)-1)/fs; % and get sampling frequency */
figure(2);
subplot(2,1,1);
plot(t,wave);
title('Wave File');
ylabel('Amplitude');
xlabel('Length (in seconds)');
L = length(wave);
NFFT = 2^nextpow2(L); % Next power of 2 from length of y
Y = fft(wave,NFFT)/L;
f = fs/2*linspace(0,1,NFFT/2+1);
% Plot single-sided amplitude spectrum.
subplot(2,1,2);
plot(f,2*abs(Y(1:NFFT/2+1)))
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
After my signal (abs value of my wav file) is convolved with the Gaussian filter i try taking the 1st and 2nd derivatives, but i don't get an output when i try to plot it.
edges=fconv(abs(song),detect);
tedges=edges(P/2:N+P/2-1);
tedges=tedges/max(abs(tedges));
W= diff(tedge);
Z= diff(W);
It is when i try to plot W and Z that I don't get the output I need. My graph is empty in other words. I can't figure out what I'm doing wrong here...
Useful: http://blogs.mathworks.com/videos/2009/12/31/basics-finding-a-subset-of-a-matrix/
Basic flow:
for v=1:window_length:length(data)
data_subsection=data(v:v+window_length);
subsection_fft = fft(data_subsection);
plot(...);
end

MATLAB - Plot time-frequency graph of .wav file

I'm working on a project that involves looking at the changes in pitch/frequency over time with a wave file (I'm new to MATLAB, but not to programming). I'm able to see the time-amplitude graph and frequency-amplitude (after an FFT) graph, but how would I be able to isolate the frequency and show it at each point in time?
Code:
filename = '/Users/Username/Sample_1.wav'
[y, fs] = wavread(filename);
y = y(:,1);
dt = 1/fs;
t = 0:dt:(length(y)*dt)-dt;
plot(t,y); xlabel('Seconds'); ylabel('Amplitude');
transformed = fft(y);
mag = abs(transformed);
plot(mag);
If you have the Signal Processing Toolbox, then you may find the spectrogram function useful.
If you don't, then you can achieve the same effect manually by calculating FFTs of consecutive (possibly overlapped) windowed segments of your time-domain data, and then plotting the amplitudes.
This is essentially the short-time Fourier transform (STFT).
If you have the Signal Processing Toolbox, spectrogram is the way to go (as Oli Charlesworth mentioned).
If you don't have it, the MATLAB Central File exchange is always a good place to look for something that general.
http://www.mathworks.com/matlabcentral/fileexchange/1553-spectrogram-short-time-ft-log-magnitude
This seems to be a sensible and well working implementation of the spectrogram functionality.

Using Matlab FFT to extract frequencies from EEG signal

I am new to BCI. I have a Mindset EEG device from Neurosky and I record the Raw data values coming from the device in a csv file. I can read and extract the data from the csv into Matlab and I apply FFT. I now need to extract certain frequencies (Alpha, Beta, Theta, Gamma) from the FFT.
Where Delta = 1-3 Hz
Theta= 4-7 Hz
Alpha = 8-12 Hz
Beta = 13-30 Hz
Gamma = 31-40 Hz
This is what I did so far:
f = (0:N-1)*(Fs/N);
plot(rawDouble);
title ('Raw Signal');
p = abs(fft(rawDouble));
figure,plot (f,p);
title('Magnitude of FFT of Raw Signal');
Can anyone tell me how to extract those particular frequency ranges from the signal?? Thank you very much!
For convenient analysis of EEG data with MatLab you might consider to use the EEGLAB toolbox (http://sccn.ucsd.edu/eeglab/) or the fieldtrip toolbox (http://fieldtrip.fcdonders.nl/start).
Both toolboxes come with good tutorials:
http://sccn.ucsd.edu/eeglab/eeglabtut.html
http://fieldtrip.fcdonders.nl/tutorial
You may find it easier to start off with MATLAB's periodogram function, rather than trying to use the FFT directly. This takes care of windowing the data for you and various other implementation details.
I think the easiest way is to filter your signal in those ranges after you load your data.
E.g.
band=[30 100] eeglocal.lowpass(band(2)).highpass(band(1));
then you can use select the time you want to process.
That should be all you need.