跪求生成粉红噪声的MATLAB程序

跪求生成粉红噪声的MATLAB程序,第1张

 MATLAB里面没有专门产生这种噪声的命令,不过你可以先用rand, randn这样的产生随机白噪声,然后用filter来滤一下。就能得到粉噪声了。

% function [pn, theta] = phase_noise(num_samp, f0, dbc_per_hz, num_taps)

%

% This function creates noise with a 1/f spectrum. The noise is then

% phase modulated so that it can be mixed with a signal to simulate

% phase noise in the original signal. The noise is specified in power

% per hertz at frequency f0, relative to the power in the carrier.

%

% References:

% N. J. Kasdin, "Discrete Simulation of Colored Noise and Stochastic

% Processes and 1/f^a Power Law Noise Generation," _Proceedings of

% the IEEE_, May, 1995.

% Roger L. Freeman, _Reference Manual for Telecommunications

% Engineering_.

% M. Schroeder, _Fractals, Chaos, and Power Laws_.

%

% Input/Output parameters:

% num_samp desired number of output samples

% f0 reference frequency (must be in Hz.)

% dbc_per_hz power per hertz relative to carrier at ref. freq.

% num_taps number of filter taps in AR 1/f filter

% (optionaldefault = 100)

%

% pn phase-modulated 1/f process

% theta 1/f process (before phase modulation)

%

% Jeff Schenck 11/21/95

%

% 1/f noise is produced by passing white noise through a filter. The

% resulting spectrum has the form

%

% Sx(w) = g^2 / w, (pretend that w is an omega)

%

% where g is the gain applied to the white noise before filtering. If P

% is the desired power in the 1 Hz. band at w0 = 2pi*f0/fs, and W is the

% 1 Hz. bandwidth in radians, we can write

%

% P = (1/2pi) Sx(w0) W

% = (1/2pi) g^2/w0 (2pi*1/fs)

% = g^2 / 2pi*f0

%

% =>g = sqrt(2pi*f0*P).

%

% Notice that the result is *independent* of fs!! Look at it this way:

% if the sampling rate is doubled for a given spectrum, the new w0 is

% half the old w0. For 1/f noise, this means that Sx(w0_new) =

% 2*Sx(w0_old). But a 1 Hz. band is half as large (in radians) than it

% was previously, so the product P is the same, and fs drops out of the

% picture.

%

% The independence with respect to fs is also an indication of the

% fractal nature of pink noise.

%

% Note that the phase-modulated noise is itself 1/f if the narrowband

% assumption is valid.

function [pn, theta] = phase_noise(num_samp, f0, dbc_per_hz, num_taps)

% Check input.

if dbc_per_hz >= 0

error('Power per Hz. must be negative.')

elseif f0 <= 0

error('Reference frequency must be positive.')

end

if nargin <4

num_taps = 100

end

% Generate white noise. Apply gain for desired dBc/Hz. Warn user

% if gain is too large (gain thresholds have been chosen somewhat

% arbitrarily -- needs work).

gain = sqrt(2*pi * f0 * 10^(dbc_per_hz/10))

wn = gain * randn(1,num_samp)

fprintf('Gain applied to white noise = %f.\n', gain)

if gain >= 1

fprintf('WARNING: Narrowband approximation no longer valid.\n')

elseif gain >= .5

fprintf('WARNING: Narrowband approximation on the verge of collapse.\n')

end

% Generate 1/f AR filter and apply to white noise to produce 1/f

% noise.

a = zeros(1,num_taps)

a(1) = 1

for ii = 2:num_taps

a(ii) = (ii - 2.5) * a(ii-1) / (ii-1)

end

theta = filter(1,a,wn)

% Phase modulate.

pn = exp(i*theta)

return

我想通过本系列文章从头到尾构建一个完整的ASP NET MVC论坛应用程序 最终的目的是探讨和推动使用ASP NET MVC框架构建应用程序的最佳实践

    简介

在本篇中 我想先从全局方面介绍一下论坛应用程序的总体目标 在本篇中 我将讨论一下避免代码坏味道的重要性 还将讨论如何利用软件设计原则和模式来帮助你编写适合未来改变的富有d性的代码 最后 我还将论证一下为什么我选择使用测试驱动开发方式构建本系列文章中的论坛应用程序

什么样的软件是好的软件

我不想仅仅为了构建论坛应用程序而任意构建此论坛应用程序 我的目标是尽可能构建最棒的论坛应用程序

这个目标立即引发这样一个问题 什么样的软件是好的软件?是什么导致一个应用程序比另一个应用程序更好一些或更差一些呢?在事先没有一个关于 好软件 的定义之前 我无法声明我构建了一个完美的论坛应用程序

因此 下面是我对于 好软件 的定义

好软件是设计得易于修改的软件

存在多种原因可能需要你改变软件

)你可能需要在一个现有软件上添加新的特征 )你可能需要修改一个现有软件中的错误 )你可能需要优化现有软件 )你可能需要改进现有软件的设计

一般说来 设计糟糕的软件是难于改变的 有些软件设计得如此糟糕 以致于每个人都害怕碰一碰它 我们大家应该都使用过设计得糟糕的软件 当软件不好时 你很希望它干脆走开 甚至如果有机会的话 你可能想从头开始重新编写这款软件

避免代码坏味道

Robert和Micah Martin把糟糕的软件部分描述为代码坏味道 下列代码坏味道意味着此软件的书写是相当糟糕的

)僵化性(Rigidity)—僵化的软件是这样的软件 当你在某个位置作一改动时即要求对系统作出相应的一系列的更改 )脆弱性(Fragility)—脆弱的软件是这样的软件 你在某个位置作一改动时即打断另外多处的正常运行 )不必要的复杂性—不必要的复杂软件是指过度设计的软件 其目的是为了处理任何可能的改变 )不必要的重复—不必要的重复软件中包含大量的重复性代码 )晦涩性—晦涩的软件是指难于理解的软件

【注意】上述这些代码味道在Micah和Robert Martin的著名《Agile Principles Patterns and Practices in C#》中得到充分的描述 在此 强烈建议读者读一下这本书 注意 上述这些代码味道都与所有的代码改变相关联 每一个这些代码味道都将妨碍代码的改变

软件设计原则

遵循良好的软件设计原则 将有助于编写软件易于适应未来更改的软件 软件设计原则有若干 也不尽相同 例如 Cunningham和Cunningham Wiki描述面向对象设计的 个原则 //c /cgi/wiki?PrinciplesOfObjectOrientedDesign

其中提到的面向对象设计的前五个原则与Robert Martin及他的儿子Micah Martin编著的《Agile Principles Patterns and Practices in C#》中所主张的软件设计原则是一致的 此外 Robert Martin还在Object Mentor开辟的博客上讨论了这些原则 // objectmentor /resources/publishedArticles

此外 我还发现有另外两本书中也提供了有关软件设计原则的极其有用的信息 第一本是Eric Freeman Elisabeth Freeman Kathy Sierra Bert Bates编著的《Head First Design Patterns》 第二本是Brett McLaughlin Gary Pollice和David West编著的《Head First Object Oriented Analysis and Design》 尽管这些书所讨论的原则与Robert Martin的提法并不十分相同 但是它们却十分相近

lishixinzhi/Article/program/net/201311/14493


欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/yw/11335545.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存