-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsfft.m
More file actions
43 lines (36 loc) · 899 Bytes
/
sfft.m
File metadata and controls
43 lines (36 loc) · 899 Bytes
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
function [f, S, P] = sfft(x, Fs, draw)
% 计算单边幅值谱
% 输入: x 信号向量, Fs 采样频率
% 输出: f 频率向量(Hz), S 单边幅值谱
if nargin < 3
draw = false;
end
N = length(x);
X = fft(x);
halfN = floor(N/2);
idx = 1:(halfN + 1);
P = angle(X(idx));
S = abs(X(idx)) / N;
% 单边谱幅值修正:DC 不翻倍;偶数长度时 Nyquist 点也不翻倍
if N > 1
if mod(N, 2) == 0
if numel(S) > 2
S(2:end-1) = 2 * S(2:end-1);
end
else
S(2:end) = 2 * S(2:end);
end
end
f = (0:halfN) * (Fs / N);
if draw
figure;
subplot(2,1,1);
eval([draw '(f, S);']);
xlabel('Frequency (Hz)'); xlim([0 Fs/2]);
ylabel('Amplitude');
title('Single-Sided Amplitude Spectrum');
subplot(2,1,2);
eval([draw '(f, rad2deg(P));']);
xlabel('Frequency (Hz)'); xlim([0 Fs/2]);
ylabel('Phase (degrees)');
end