东方耀AI技术分享

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 5076|回复: 0
打印 上一主题 下一主题

[Python] 如何正确看信号的频谱(fft,fftshift)确定频谱横轴

[复制链接]

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14429
QQ
跳转到指定楼层
楼主
发表于 2021-8-31 14:17:34 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
# 如何正确看信号的频谱(fft,fftshift)确定频谱横轴
# 在FFT里fftshift的作用:通过将零频分量移动到数组中心
# 复序列没有负频率,采样率只要大于带宽即可


  1. close all; clear; clc;
  2. fs = 300;            %采样率大于200
  3. t = 0:1/fs:1;        %定义采样点
  4. s = cos(2*pi*100*t)+cos(2*pi*40*t);  %采样后的信号序列

  5. %---------看[0,fs]的频谱---------
  6. F1 = fft(s);                %fft
  7. f1 = linspace(0,fs,length(t));   %频谱横轴
  8. subplot(211);plot(f1,abs(F1));xlabel('f');ylabel('幅度');title('看[0,fs]');

  9. %---------看[-fs/2,fs/2]的频谱---------
  10. F2 = fftshift(fft(s));                %fft
  11. f2 = linspace(0,fs,length(t))-fs/2;   %频谱横轴
  12. subplot(212);plot(f2,abs(F2));xlabel('f');ylabel('幅度');title('看[-fs/2,fs/2]');
复制代码

  1. # -*- coding: utf-8 -*-
  2. __author__ = u'东方耀 微信:dfy_88888'
  3. __date__ = '2021/8/31 上午10:17'
  4. __product__ = 'PyCharm'
  5. __filename__ = '19_单频多频fft'

  6. from scipy.fftpack import fft, ifft, fftshift
  7. import numpy as np
  8. import matplotlib.pyplot as plt
  9. import matplotlib.ticker as mticker
  10. from matplotlib.font_manager import FontProperties

  11. font_fname = "/usr/share/fonts/wps-office/simfang.ttf"
  12. font = FontProperties(fname=font_fname)
  13. # plt.rcParams['font.sans-serif']=['simfang']#设置作图中文显示

  14. # 如何正确看信号的频谱(fft,fftshift)确定频谱横轴
  15. # 在FFT里fftshift的作用:通过将零频分量移动到数组中心
  16. # 复序列没有负频率,采样率只要大于带宽即可


  17. def gen_signal(prt, f, f_sample, amplitude=1, is_complex=False):
  18.     """
  19.     信号生成
  20.     :param prt: 脉冲持续时间 脉宽
  21.     :param f:   信号频率分量  list
  22.     :param f_sample:  采样率
  23.     :return:   信号波形 x y
  24.     """
  25.     sample_point_num = int(np.ceil(prt * f_sample))
  26.     print("子脉冲的采样点数:", sample_point_num)
  27.     x_time = np.linspace(0, prt, sample_point_num)
  28.     y_signal = np.zeros(shape=[len(x_time), ])
  29.     if not is_complex:
  30.         # 实信号
  31.         for f_item in f:
  32.             print("频率分量={}的信号一个周期的采样点数={}".format(f_item, f_sample/f_item))
  33.             y_signal += amplitude * np.cos(2 * np.pi * f_item * x_time)
  34.     return x_time, y_signal


  35. fs = 300
  36. x_time, y_signal = gen_signal(1, [100,40, ], fs)
  37. fft_y = fft(y_signal)
  38. # 频谱横轴 看[0,fs]
  39. x_f = np.linspace(0, int(fs), len(y_signal), endpoint=True)

  40. # 在FFT里fftshift的作用:通过将零频分量移动到数组中心
  41. fft_y_shift = fftshift(fft_y)
  42. # 复序列没有负频率,采样率只要大于带宽即可
  43. # 频谱横轴 看[-fs/2,fs/2]
  44. x_f_shift = np.linspace(0, int(fs), len(y_signal), endpoint=True) - int(fs/2)
  45. # print("归一化后的幅度谱的最大值:", np.max(abs_y_norm))


  46. # 画图展示
  47. plt.figure(figsize=(14, 12))
  48. # seen_point_num = 28
  49. plt.figure(1)

  50. # plt.subplot(2, 1, 1)
  51. # plt.plot(y_signal[:seen_point_num], marker='o', markersize=8, markerfacecolor='r')
  52. # plt.title('发射信号_时域(实部)', fontproperties=font, fontsize=16)
  53. # # plt.axis([0, 500, -3, 3])
  54. # plt.xlabel('time/sample_num')

  55. plt.subplot(2, 1, 1)
  56. plt.plot(x_f, np.abs(fft_y), 'blue')
  57. plt.title('幅度谱(未归一化)看[0,fs]', fontproperties=font, fontsize=16)
  58. # plt.axis([0, 500, -3, 3])
  59. plt.xlabel('freq')

  60. plt.subplot(2, 1, 2)
  61. plt.plot(x_f_shift, np.abs(fft_y_shift), 'blue')
  62. plt.title('幅度谱(未归一化)看[-fs/2,fs/2]', fontproperties=font, fontsize=16)
  63. # plt.axis([0, 500, -3, 3])
  64. plt.xlabel('freq')


  65. plt.show()

复制代码





看频谱01.png (42.03 KB, 下载次数: 106)

看频谱01.png

看频谱02.png (58.17 KB, 下载次数: 107)

看频谱02.png
让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|人工智能工程师的摇篮 ( 湘ICP备2020019608号-1 )

GMT+8, 2024-4-20 15:13 , Processed in 0.179729 second(s), 21 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表