-
Notifications
You must be signed in to change notification settings - Fork 0
/
power_spectrum.m
78 lines (61 loc) · 2.04 KB
/
power_spectrum.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
%% Power Spectrum from FFT and Welch's Method
%% Create a Signal
% Signal parameters
srate = 1000; % sampling rate in Hz
time = -2:1/srate:2;
npnts = length(time);
frex = [ 5 8 16 20 27 ];
ampl = [ 7 6 2 4 1 ];
% Initialize signal
signal = zeros(1,length(time));
for fi=1:length(frex)
AM = ampl(fi)*interp1(randn(20,1),linspace(1,20,length(time)),'pchip');
signal = signal + AM.*sin(2*pi*frex(fi)*time);
end
% Let's look at how the signal looks like
figure(1)
plot(time,signal,'k')
xlabel('Time'), ylabel('Amplitude')
%% "Static" Power Spectrum from FFT
% Vector of frequencies
hz = linspace(0,srate/2,floor(npnts/2)+1);
% Compute signal power
sigpower = abs(fft(signal)/npnts).^2;
% Plot the power spectrum
figure(2)
subplot(211)
plot(hz,sigpower(1:length(hz)),'k','linew',2)
set(gca,'xlim',[0 max(frex)+10])
xlabel('Frequency (Hz)'), ylabel('Power')
title('Power spectrum from FFT')
% Show dashed vertical lines with simulated frequencies
hold on
plot(repmat(frex,2,1),repmat(get(gca,'ylim')',1,length(frex)),'r--');
%% Welch's Method
% Window length in time points
winlength = 1000;
% Window onset times
winonsets = round(linspace(1,npnts-winlength,50));
% Different-length signal needs a different-length Hz vector
hzW = linspace(0,srate/2,floor(winlength/2)+1);
% Initialize the power matrix
sigpower2 = zeros(length(winonsets),length(hzW));
% Loop over frequencies
for wi=1:length(winonsets)
% Get a chunk of data from this time window
datachunk = signal(winonsets(wi):winonsets(wi)+winlength);
% Compute its power
tmppow = abs(fft(datachunk)/winlength).^2;
% Enter into matrix
sigpower2(wi,:) = tmppow(1:length(hzW));
end
% Plot the power spectrum
subplot(212)
plot(hzW,mean(sigpower2,1),'k','linew',2)
set(gca,'xlim',[0 max(frex)*2])
xlabel('Frequency (Hz)'), ylabel('Power')
title('Power spectrum from Welch''s method')
% Show simulated frequencies
hold on
plot(repmat(frex,2,1),repmat(get(gca,'ylim')',1,length(frex)),'r--');
%% end