-
Notifications
You must be signed in to change notification settings - Fork 49
/
shapeSpectrum.m
63 lines (50 loc) · 1.58 KB
/
shapeSpectrum.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
function [ y_shaped ] = shapeSpectrum( y, spectrum, freqs, fs )
% This function will shape an input signal to the given spectrum (simple, unregulated spectral shaping)
%
% Syntax: [ y_shaped ] = shapeSpectrum( y, fs, spectrum )
%
% Inputs:
% y - Input signal in the time-domain which requires shaping.
% spectrum - A vector containing the magnitude spectrum to shape the
% signal with. ( length = fs/2 - 1 )
% freqs - A vector containing the corresponding frequencies for the
% spectrum vector.
%
% Outputs:
% y_shaped - The input signal shaped to the given spectrum
%
%
% Author: Jacob Donley
% University of Wollongong
% Email: jrd089@uowmail.edu.au
% Copyright: Jacob Donley 2017
% Date: 26 February 2016
% Revision: 0.1
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
N = length( y );
if rem(N,2)
M = N+1;
else
M = N;
end
% Frequency Domain Tranform
Y = fft(y); % Fast Fourier Transform
% Frequencies
NumPts = M/2 + 1;
freqs_new = linspace(0, fs/2, NumPts);
% Create correct size spectrum vector
spect = interp1( freqs, spectrum, freqs_new )';
% Apply magnitude weighting
Y(1:NumPts) = Y(1:NumPts) .* spect;
% Apply conjugation for negative frequency side of spectrum
Y(NumPts+1:M) = conj(Y(M/2:-1:2));
% Time Domain Transform
y_shaped = ifft(Y); % Inverse Fast Fourier Transform
% prepare output vector y
y_shaped = real(y_shaped(1:N));
% ensure unity standard deviation and zero mean value
y_shaped = y_shaped - mean(y_shaped);
y_shaped = y_shaped / rms(y_shaped);
% BETTER METHOD %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end