# FFT

## 摘要

1. 用数字方式（离散时间）描述信号$$x(t)$$
2. 利用傅里叶变换将离散信号$$x[n]$$变换到频域$$X[k]$$
3. 提取频域信号$$X[k]$$的幅值和相位谱，及功率谱
4. 由频域信号恢复出时域信号

## 离散时间域表示

$\begin{equation}\label{eq:xt} x(t)=4cos(2\pi 2t+\pi/4) \end{equation}$

A=4;
f=2;
phi=pi/4;
duration=2;
fs=4*f;
t=0:1/fs:duration-1/fs;
x=A*cos(2*pi*f*t+phi);
figure('Position',[500 300 400 300])
subplot(2,1,1)
plot(t,x)
hold on
stem(t,x,':.','color',[0.5 0.5 0.5])
ylabel('fs=8')
hold off
fs=16*f;
t=0:1/fs:duration-1/fs;
x=A*cos(2*pi*f*t+phi);
subplot(2,1,2)
plot(t,x)
hold on
stem(t,x,':.','color',[0.5 0.5 0.5])
xlabel('time(s)')
ylabel('fs=32') ## 利用傅立叶变换到频域

N = 32;  % Transform length, FFT size
X=fft(x,N);
ff = fs*(0:N-1)/N;
subplot(3,1,2)
stem(ff,abs(X)/N,'.');


• N=8，频率信息失真
• N=32，校准确地获得频率信息
• N=128，？？

X = fftshift(fft(x,N))/N;
stem(ff,abs(X),'.'); %magnitudes vs frequencies
xlabel('Frequency (Hz)');
ylabel('Amplitude |X[N]|');
power=X.*conj(X);
stem(ff,power,'.')
xlabel('Frequency (Hz)');
ylabel('Power'); subplot(2,1,1)
plot(ff,angle(X)*180/pi); %magnitudes vs frequencies
X2=X;%store the FFT results in another array
%detect noise (very small numbers (eps)) and ignore them
threshold = max(abs(X))/10000; %tolerance threshold
X2(abs(X)<threshold) = 0; %maskout values that are below the threshold
phase=unwrap(angle(X2))*180/pi; %phase information
subplot(2,1,2)
stem(ff,phase,'.'); %phase vs frequencies
{"ft_lang_label":"__label__zh","ft_lang_prob":0.5182615,"math_prob":0.99986327,"size":2027,"snap":"2023-14-2023-23","text_gpt3_token_len":1232,"char_repetition_ratio":0.08897677,"word_repetition_ratio":0.02739726,"special_character_ratio":0.35668474,"punctuation_ratio":0.18558952,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9999312,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,3,null,3,null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-06-09T22:35:45Z\",\"WARC-Record-ID\":\"<urn:uuid:8516eaa1-cb8d-446d-85eb-162b45205f7f>\",\"Content-Length\":\"68541\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:fdf7d8d0-9a23-46ea-a0e4-339863932416>\",\"WARC-Concurrent-To\":\"<urn:uuid:6437b0b1-dd99-4be4-b65f-b3eeeeb5d581>\",\"WARC-IP-Address\":\"81.68.94.171\",\"WARC-Target-URI\":\"https://mekesim.com/doc/ESP32/FFT/\",\"WARC-Payload-Digest\":\"sha1:NQBC6LPXJZV44JSJM6EUI6N3OD65VDPO\",\"WARC-Block-Digest\":\"sha1:5DL235PB34LWGSDXL5MUSW6OWVHGUATL\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-23/CC-MAIN-2023-23_segments_1685224656833.99_warc_CC-MAIN-20230609201549-20230609231549-00583.warc.gz\"}"}