File: DFT_time2freq.m

package info (click to toggle)
openems 0.0.35+dfsg.1-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 6,288 kB
  • sloc: cpp: 40,259; yacc: 580; lex: 350; makefile: 258; sh: 169; ruby: 19
file content (47 lines) | stat: -rw-r--r-- 1,076 bytes parent folder | download | duplicates (2)
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
function f_val = DFT_time2freq( t, val, freq, signal_type )
% f_val = DFT_time2freq( t, val, freq, signal_type )
%
% computes the DFT at the given frequencies
%
% parameter:
% t  : time vector
% val: data vector
% freq: DFT frequency vector
% signal_type: 'pulse' (default), 'periodic'
%
% return values:
% f_val:  single-sided spectrum
%
% example:
%   t=linspace(0,1,100);
%   t_val=0.9*sin(2*pi*3*t); % sine wave; amplitude 0.9; frequency 3 Hz
%   f=linspace(1,5,101);
%   f_val=DFT_time2freq( t, t_val, f, 'periodic' );
%   interp1(f,abs(f_val),3)
% ans = 0.8910
%   plot( t, t_val )
%   plot( f, abs(f_val) )

if numel(t) ~= numel(val)
    error 'numel(t) ~= numel(val)'
end

if nargin<4
  signal_type = 'pulse';
end

f_val = zeros(1,numel(freq));
for f_idx=1:numel(freq)
    f_val(f_idx) = sum( val .* exp( -1i * 2*pi*freq(f_idx) * t ) );
end

if strcmpi(signal_type, 'pulse')
  dt = t(2)-t(1);
  f_val = f_val * dt;
elseif strcmpi(signal_type, 'periodic')
  f_val = f_val / length(t);
else
  error 'unknown signal type'
end
 
f_val = f_val * 2; % single-sided spectrum