|
# 如何正确看信号的频谱(fft,fftshift)确定频谱横轴
# 在FFT里fftshift的作用:通过将零频分量移动到数组中心
# 复序列没有负频率,采样率只要大于带宽即可
- close all; clear; clc;
- fs = 300; %采样率大于200
- t = 0:1/fs:1; %定义采样点
- s = cos(2*pi*100*t)+cos(2*pi*40*t); %采样后的信号序列
- %---------看[0,fs]的频谱---------
- F1 = fft(s); %fft
- f1 = linspace(0,fs,length(t)); %频谱横轴
- subplot(211);plot(f1,abs(F1));xlabel('f');ylabel('幅度');title('看[0,fs]');
- %---------看[-fs/2,fs/2]的频谱---------
- F2 = fftshift(fft(s)); %fft
- f2 = linspace(0,fs,length(t))-fs/2; %频谱横轴
- subplot(212);plot(f2,abs(F2));xlabel('f');ylabel('幅度');title('看[-fs/2,fs/2]');
复制代码
- # -*- coding: utf-8 -*-
- __author__ = u'东方耀 微信:dfy_88888'
- __date__ = '2021/8/31 上午10:17'
- __product__ = 'PyCharm'
- __filename__ = '19_单频多频fft'
- from scipy.fftpack import fft, ifft, fftshift
- import numpy as np
- import matplotlib.pyplot as plt
- import matplotlib.ticker as mticker
- from matplotlib.font_manager import FontProperties
- font_fname = "/usr/share/fonts/wps-office/simfang.ttf"
- font = FontProperties(fname=font_fname)
- # plt.rcParams['font.sans-serif']=['simfang']#设置作图中文显示
- # 如何正确看信号的频谱(fft,fftshift)确定频谱横轴
- # 在FFT里fftshift的作用:通过将零频分量移动到数组中心
- # 复序列没有负频率,采样率只要大于带宽即可
- def gen_signal(prt, f, f_sample, amplitude=1, is_complex=False):
- """
- 信号生成
- :param prt: 脉冲持续时间 脉宽
- :param f: 信号频率分量 list
- :param f_sample: 采样率
- :return: 信号波形 x y
- """
- sample_point_num = int(np.ceil(prt * f_sample))
- print("子脉冲的采样点数:", sample_point_num)
- x_time = np.linspace(0, prt, sample_point_num)
- y_signal = np.zeros(shape=[len(x_time), ])
- if not is_complex:
- # 实信号
- for f_item in f:
- print("频率分量={}的信号一个周期的采样点数={}".format(f_item, f_sample/f_item))
- y_signal += amplitude * np.cos(2 * np.pi * f_item * x_time)
- return x_time, y_signal
- fs = 300
- x_time, y_signal = gen_signal(1, [100,40, ], fs)
- fft_y = fft(y_signal)
- # 频谱横轴 看[0,fs]
- x_f = np.linspace(0, int(fs), len(y_signal), endpoint=True)
- # 在FFT里fftshift的作用:通过将零频分量移动到数组中心
- fft_y_shift = fftshift(fft_y)
- # 复序列没有负频率,采样率只要大于带宽即可
- # 频谱横轴 看[-fs/2,fs/2]
- x_f_shift = np.linspace(0, int(fs), len(y_signal), endpoint=True) - int(fs/2)
- # print("归一化后的幅度谱的最大值:", np.max(abs_y_norm))
- # 画图展示
- plt.figure(figsize=(14, 12))
- # seen_point_num = 28
- plt.figure(1)
- # plt.subplot(2, 1, 1)
- # plt.plot(y_signal[:seen_point_num], marker='o', markersize=8, markerfacecolor='r')
- # plt.title('发射信号_时域(实部)', fontproperties=font, fontsize=16)
- # # plt.axis([0, 500, -3, 3])
- # plt.xlabel('time/sample_num')
- plt.subplot(2, 1, 1)
- plt.plot(x_f, np.abs(fft_y), 'blue')
- plt.title('幅度谱(未归一化)看[0,fs]', fontproperties=font, fontsize=16)
- # plt.axis([0, 500, -3, 3])
- plt.xlabel('freq')
- plt.subplot(2, 1, 2)
- plt.plot(x_f_shift, np.abs(fft_y_shift), 'blue')
- plt.title('幅度谱(未归一化)看[-fs/2,fs/2]', fontproperties=font, fontsize=16)
- # plt.axis([0, 500, -3, 3])
- plt.xlabel('freq')
- plt.show()
复制代码
|
|