# Hi there, i just want to design and apply a 2s hanning window with 50% overlapp on my EEG signal in order to segment it , i've tried with @hann command but i couldn't get it

13 views (last 30 days)
alireza ghavami on 17 Jan 2023
Commented: alireza ghavami on 19 Feb 2023
my signal is a 17-channel signal with 30721 samples totally (=17 * 30721) . i need my signal to be segmented with 2s hanning window with 50% overlapp to apply preproccessing on it for my Master thesis. sampling frequency is 256 Hz.
i have problem from A to Z designing the window and also, i dont know how to apply it on my Original Signal,
I will be grateful if you could help me design and apply this window to my signal,
thank you so much

Mathieu NOE on 23 Jan 2023
hello
see example below
the signal gets splitted in 2s buffer and windowed
see the subfunction at the bottom of the code :
sw = signal(start:stop).*window; % signal buffered and windowed
then we do fft on each windowed segment and when iteration process is complete we get a time / frequency (spectrgram) plot
code :
clearvars
% dummy signal
Fs = 256;
dt = 1/Fs;
t = 0:dt:30; % 2 s at 1 kHz sample rate
signal = chirp(t,0,15,50,'quadratic'); % Start at DC, cross 50 Hz at 15 s
nfft = 2*256; % frame length = 2 seconds x Fs
overlap = 0.5; % window overlap ; 0 = 0 % , 0.5 = 50% , 1 = 100 %
[S,F,T] = myspecgram(signal, Fs, nfft, overlap);
figure(1);
imagesc(T,F,20*log10(abs(S)))
colorbar('vert');
set(gca,'YDir','Normal')
xlabel('Time (secs)')
ylabel('Freq (Hz)')
title('Short-time Fourier Transform spectrum (dB Scale)')
colormap('jet');
function [fft_specgram,freq_vector,time] = myspecgram(signal, Fs, nfft, Overlap)
% FFT peak spectrogram of signal (example sinus amplitude 1 = 0 dB after fft).
% signal - input signal,
% Fs - Sampling frequency (Hz).
% nfft - FFT window size
% Overlap - buffer overlap % (between 0 and 0.95)
signal = signal(:);
samples = length(signal);
% fill signal with zeros if its length is lower than nfft
if samples<nfft
s_tmp = zeros(nfft,1);
s_tmp((1:samples),:) = signal;
signal = s_tmp;
samples = nfft;
end
% window : hanning
window = hanning(nfft);
window = window(:);
% compute fft with overlap
offset = fix((1-Overlap)*nfft);
spectnum = 1+ fix((samples-nfft)/offset); % Number of windows
% % for info is equivalent to :
% noverlap = Overlap*nfft;
% spectnum = fix((samples-noverlap)/(nfft-noverlap)); % Number of windows
% main loop
fft_specgram = [];
for ci=1:spectnum
start = 1+(ci-1)*offset;
stop = start+nfft-1;
sw = signal(start:stop).*window; % signal buffered and windowed
fft_specgram = [fft_specgram abs(fft(sw))*4/nfft]; % X=fft(x.*hanning(N))*4/N; % hanning only
end
% one sidded fft spectrum % Select first half
if rem(nfft,2) % nfft odd
select = (1:(nfft+1)/2)';
else
select = (1:nfft/2+1)';
end
fft_specgram = fft_specgram(select,:);
freq_vector = (select - 1)*Fs/nfft;
% time vector
% time stamps are defined in the middle of the buffer
time = ((0:spectnum-1)*offset + round(nfft/2))/Fs;
end
alireza ghavami on 19 Feb 2023
hi dear Mathieu , im so sorry to reply you late,
im very thankful for your help, you helped me alot
i was trying to read about your last comment in the help of MATLAB but i did not get it clearly,
and if its possible, can i ask you to write me your contact details so i could contact you easier?
im so sorry if im asking too much questions :)

### Categories

Find more on Measurements and Feature Extraction in Help Center and File Exchange

R2016b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!